Skip to content

Fix possible null deref in pub - #848

Merged
levb merged 1 commit into
nats-io:mainfrom
wooffie:fix-pub-null
Feb 28, 2025
Merged

Fix possible null deref in pub#848
levb merged 1 commit into
nats-io:mainfrom
wooffie:fix-pub-null

Conversation

@wooffie

@wooffie wooffie commented Feb 21, 2025

Copy link
Copy Markdown
Contributor
  1. remove reply construct because it is done by natsConn_publish

    reply = msg->reply;

  2. we have check msg != NULL and i just move it to natsConn_publish

    const char *reply = (msg != NULL ? msg->reply : NULL);

Comment thread src/pub.c Outdated
int totalLen = 0;

if (nc == NULL)
if (nc == NULL || msg == NULL)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because normally the caller ensures that it is not NULL. The only exception was natsConnection_PublishMsg() that needs fixing.

The rationale is that this is an internal function and if the caller guarantees that the message is not NULL, then we should not check in this one, which makes it redundant. In lots of places, the caller uses a stack variable and it will never be NULL.

Comment thread src/pub.c
natsStatus
natsConnection_PublishMsg(natsConnection *nc, natsMsg *msg)
{
const char *reply = (msg != NULL ? msg->reply : NULL);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to rewrite this function and if msg == NULL return an invalid arg. The check of nc is done in the lower function, but could easily be added into this one.

As for removing the reply subject, of course we need it, but yes, the called function will take it from the provided message structure if not provided as a parameter, so I guess this change is ok.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kozlovic "rewrite this function" - you mean add the check/return before msg is even used and remove any conditionals like this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant what the contributor did, that is, check for msg == NULL as an invalid arg and return if that is the case. He did add the check for nc == NULL (although that is done in the internal function so that was not entirely necessary).

@kozlovic kozlovic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@levb
levb merged commit d8345b6 into nats-io:main Feb 28, 2025
levb pushed a commit that referenced this pull request Feb 28, 2025
@levb levb removed the cherry-pick label Feb 28, 2025
@levb levb mentioned this pull request Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants