Skip to content

Fix coverity unbounded source buffer issues - #989

Merged
alejandro-colomar merged 1 commit into
shadow-maint:masterfrom
MarcinDigitic:topic/coverity/fix_unbound_input_buffer
Oct 22, 2024
Merged

alejandro-colomar merged 1 commit into
shadow-maint:masterfrom
MarcinDigitic:topic/coverity/fix_unbound_input_buffer

Conversation

@MarcinDigitic

@MarcinDigitic MarcinDigitic commented May 10, 2024 •

Copy link
Copy Markdown
Contributor

Fix coverity unbound buffer issues

During coverity scan, there are reported four issues
with unbounded source buffer for each usage of input arg
directly with syslog function.

Sample coverity test report for chsh.c file:

  1. string_size_argv: argv contains strings with unknown size.
    int main (int argc, char **argv)
    [...]
  2. var_assign_var: Assigning: user = argv[optind]. Both are now tainted.
    user = argv[optind];
    [...]
    CID 5771784: (Retiring certain usernames. #1 of 1): Unbounded source buffer (STRING_SIZE)
  3. string_size: Passing string user of unknown size to syslog.
    SYSLOG ((LOG_INFO, "changed user '%s' shell to '%s'", user, loginsh));

Similar issue is reported three times more:
File: chfn.c, function: main, variable: user
File: passwd.c, function: main, variable: name
File: newgrp.c, function: main, variable: group

The proposed commit is a try to fix the reported issues
by adding a check for a valid user or group names.

Comment thread lib/alloc.c Resolved
Comment thread lib/alloc.c Resolved
Comment thread src/chfn.c Resolved
Comment thread lib/defines.h Resolved
Comment thread lib/alloc.c Resolved
Comment thread lib/alloc.c Resolved
@alejandro-colomar

alejandro-colomar commented May 10, 2024 •

Copy link
Copy Markdown
Collaborator

During coverity scan, there is reported an issue with unbounded source buffer for each usage of input arg directly with syslog function.

Please paste such a Coverity scan report in the commit message. It would be helpful when reviewing.

alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request May 10, 2024
It encapsulates some logic that we may want to reuse elsewhere.

Link: <shadow-maint#989>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request May 10, 2024
It encapsulates some logic that we may want to reuse elsewhere.

Link: <shadow-maint#989>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request May 10, 2024
It encapsulates some logic that we may want to reuse elsewhere.

Link: <shadow-maint#989>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Comment thread src/chsh.c Resolved
ikerexxe pushed a commit that referenced this pull request May 21, 2024
It encapsulates some logic that we may want to reuse elsewhere.

Link: <#989>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@alejandro-colomar

alejandro-colomar commented May 30, 2024 •

Copy link
Copy Markdown
Collaborator

Can you please rebase the branch?


Edit: I've done it myself for you.

v1b:

  • Rebase
$ git range-diff md/master..md/topic/coverity/fix_unbound_input_buffer shadow/master..989 
1:  a90453e6 = 1:  bb741102 Fix coverity unbound buffer issues

@alejandro-colomar
alejandro-colomar force-pushed the topic/coverity/fix_unbound_input_buffer branch from a90453e to bb74110 Compare May 30, 2024 10:29
@alejandro-colomar

Copy link
Copy Markdown
Collaborator

Please address the issues raised.

Comment thread lib/alloc.c Resolved
Comment thread lib/alloc.c Resolved
Comment thread lib/defines.h Resolved
@alejandro-colomar

Copy link
Copy Markdown
Collaborator

I've converted your PR to draft, since it's not ready for review. Please address the comments.

@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from bb74110 to 7b8d2f7 Compare June 25, 2024 07:05
Comment thread src/chfn.c Resolved
Comment thread src/chfn.c Resolved
Comment thread src/chfn.c Resolved
Comment thread src/chfn.c Resolved
Comment thread src/chfn.c Resolved
Comment thread src/newgrp.c Resolved
@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 7b8d2f7 to 21af660 Compare June 25, 2024 07:28
Comment thread src/chfn.c Resolved
@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 21af660 to 1351181 Compare June 25, 2024 07:45
Comment thread src/chfn.c Resolved
@alejandro-colomar

Copy link
Copy Markdown
Collaborator

Hi, would you mind updating the PR?

@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch 2 times, most recently from dfa4448 to 3276c83 Compare October 14, 2024 08:28
@MarcinDigitic

Copy link
Copy Markdown
Contributor Author

Hi, would you mind updating the PR?

Hi @alejandro-colomar , the PR is cleaned and rebased -> it should be ready for merging.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator

Does this change remove all of the coverity reports? Which of them are still present after this change?

@MarcinDigitic

Copy link
Copy Markdown
Contributor Author

Does this change remove all of the coverity reports? Which of them are still present after this change?

This will not remove the coverity reports issues. To remove the coverity report errors we would need to use xstrdup as it was in my original code. However, in this case I will prepare a separate PR after merging this one.
And also, after adding the conditions as in this PR we would be safe to ignore the coverity reports issues.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator

Does this change remove all of the coverity reports? Which of them are still present after this change?

This will not remove the coverity reports issues. To remove the coverity report errors we would need to use xstrdup as it was in my original code.

Then the commit message should be updated to reflect what this commit does.

@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 3276c83 to 28aed99 Compare October 14, 2024 10:31
@MarcinDigitic

Copy link
Copy Markdown
Contributor Author

The commit message is updated.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator
The proposed changes add conditions verifing size of passed arguments
for users and groups names.

I don't understand that sentence. Could you please rephrase?

@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 28aed99 to 6279ad6 Compare October 14, 2024 10:43
@MarcinDigitic

Copy link
Copy Markdown
Contributor Author

Please, check now. The main reported issue by coverity is:

1. string_size_argv: argv contains strings with unknown size.
     int main (int argc, char **argv)
    [...]
     4. var_assign_var: Assigning: user = argv[optind]. Both are now tainted.
     user = argv[optind];
    [...]
    CID 5771784: (#1 of 1): Unbounded source buffer (STRING_SIZE)
    15. string_size: Passing string user of unknown size to syslog.
     SYSLOG ((LOG_INFO, "changed user '%s' shell to '%s'", user, loginsh));

Passing string user of unknown size to syslog
The added conditions check also the string sizes of the passed arguments.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator

Please, check now.

The proposed changes add conditions, which verify
the user and group names arguments, including their sizes.

You should say lengths instead of sizes. is_valid_user_name() verifies the string length (strlen(3)).

$ grepc is_valid_user_name .
./lib/chkname.h:extern bool is_valid_user_name (const char *name);
./lib/chkname.c:bool
is_valid_user_name(const char *name)
{
	if (strlen(name) >= login_name_max_size()) {
		errno = EOVERFLOW;
		return false;
	}

	return is_valid_name(name);
}

@alejandro-colomar

Copy link
Copy Markdown
Collaborator
This will not fix the coverity reports, but the change causes
that they are irrelevant and could be ignored.

The report is actually fixed, I think. What the patch doesn't do is "silence" it. Please reword. Other than that, it more or less LGTM.

@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 6279ad6 to ca15d35 Compare October 14, 2024 12:06
@MarcinDigitic

Copy link
Copy Markdown
Contributor Author

Wording is corrected.

Comment thread src/chfn.c Resolved
Comment thread src/chsh.c Resolved
During coverity scan, there are reported four issues
with unbounded source buffer for each usage of input arg
directly with syslog function.

Sample coverity test report for chsh.c file:

 1. string_size_argv: argv contains strings with unknown size.
 int main (int argc, char **argv)
[...]
 4. var_assign_var: Assigning: user = argv[optind]. Both are now tainted.
 user = argv[optind];
[...]
CID 5771784: (shadow-maint#1 of 1): Unbounded source buffer (STRING_SIZE)
15. string_size: Passing string user of unknown size to syslog.
 SYSLOG ((LOG_INFO, "changed user '%s' shell to '%s'", user, loginsh));

Similar issue is reported three times more:
File: chfn.c, function: main, variable: user
File: passwd.c, function: main, variable: name
File: newgrp.c, function: main, variable: group

This commit is the first approach to fix the reported issues.
The proposed changes add conditions, which verify
the user and group names arguments, including their lengths.
This will not silence the coverity reports, but the change causes
that they are irrelevant and could be ignored.
@MarcinDigitic
MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from ca15d35 to e381b2d Compare October 16, 2024 12:49

@alejandro-colomar alejandro-colomar left a comment

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.

LGTM. Thanks!

@MarcinDigitic
MarcinDigitic marked this pull request as ready for review October 16, 2024 13:13

@ikerexxe ikerexxe left a comment

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.

LGTM!

Alejandro, do you also want hallyn's feedback? If not, you can proceed to merge it.

@alejandro-colomar

Copy link
Copy Markdown
Collaborator

LGTM!

Thanks!

Alejandro, do you also want hallyn's feedback? If not, you can proceed to merge it.

Nah, I just wanted another pair of eyes to make sure I wasn't forgetting something. Thank you!

@alejandro-colomar
alejandro-colomar merged commit 326889c into shadow-maint:master Oct 22, 2024
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