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

security: fix sscanf rv checks (CodeQL) #6184

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
security: fix sscanf rv checks (CodeQL)
Fix the following CodeQL warning (CWE-253)[1]:

> Rule ID: cpp/incorrectly-checked-scanf

> The result of scanf is only checked against 0, but it can also return
> EOF.

> Functions in the scanf family return either EOF (a negative value) in
> case of IO failure, or the number of items successfully read from the
> input.  Consequently, a simple check that the return value is nonzero
> is not enough.
>
> Recommendation
>
> Ensure that all uses of scanf check the return value against the
> expected number of arguments rather than just against zero.

Note: The affected code portions attempt to read values from /etc/passwd
and /etc/group, so invalid input seems unlikely to be the case.  Either
way, the changes make the checks in question more consistent with
similar sscanf return value checks in the rest of the code.

Added on commit 4f003da ("prevent leaking user information by
modifying /home directory, /etc/passwd and /etc/group", 2015-11-19).

[1] https://github.com/netblue30/firejail/security/code-scanning/32
  • Loading branch information
kmk3 committed Feb 2, 2024
commit ef2f5f384697f8227a7c0d3ef11c2f158c6acad0
8 changes: 4 additions & 4 deletions src/firejail/restrict_users.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ static void sanitize_passwd(void) {
goto errout;

// process uid
int uid;
int uid = -1;
int rv = sscanf(ptr, "%d:", &uid);
if (rv == 0 || uid < 0)
if (rv != 1 || uid < 0)
goto errout;
assert(uid_min);
if (uid < uid_min || uid == 65534) { // on Debian platforms user nobody is 65534
Expand Down Expand Up @@ -349,9 +349,9 @@ static void sanitize_group(void) {
goto errout;

// process uid
int gid;
int gid = -1;
int rv = sscanf(ptr, "%d:", &gid);
if (rv == 0 || gid < 0)
if (rv != 1 || gid < 0)
goto errout;
assert(gid_min);
if (gid < gid_min || gid == 65534) { // on Debian platforms 65534 is group nogroup
Expand Down
Loading