Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverity unbounded source buffer issues #989

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MarcinDigitic
Copy link

@MarcinDigitic MarcinDigitic commented May 10, 2024

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.

lib/alloc.c Outdated Show resolved Hide resolved
lib/alloc.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
lib/defines.h Outdated Show resolved Hide resolved
lib/alloc.c Outdated Show resolved Hide resolved
lib/alloc.c Outdated Show resolved Hide resolved
@alejandro-colomar
Copy link
Collaborator

alejandro-colomar commented May 10, 2024

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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
src/chsh.c Outdated Show resolved Hide 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 <[email protected]>
@alejandro-colomar
Copy link
Collaborator

alejandro-colomar commented May 30, 2024

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
Collaborator

Please address the issues raised.

lib/alloc.c Outdated Show resolved Hide resolved
lib/alloc.c Outdated Show resolved Hide resolved
lib/defines.h Outdated Show resolved Hide resolved
@alejandro-colomar
Copy link
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
src/chfn.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
src/newgrp.c Outdated Show resolved Hide resolved
@MarcinDigitic MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 7b8d2f7 to 21af660 Compare June 25, 2024 07:28
src/chfn.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
src/chfn.c Outdated Show resolved Hide resolved
@MarcinDigitic MarcinDigitic force-pushed the topic/coverity/fix_unbound_input_buffer branch from 21af660 to 1351181 Compare June 25, 2024 07:45
src/chfn.c Outdated Show resolved Hide resolved
@alejandro-colomar
Copy link
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
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
Collaborator

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

@MarcinDigitic
Copy link
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
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
Author

The commit message is updated.

@alejandro-colomar
Copy link
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
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
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
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
Author

Wording is corrected.

src/chsh.c Outdated Show resolved Hide 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
Copy link
Collaborator

@alejandro-colomar alejandro-colomar left a comment

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
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.

2 participants