Skip to content

Commit

Permalink
bugfix: fix sscanf rv checks (CodeQL)
Browse files Browse the repository at this point in the history
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 1, 2024
1 parent bb45aa5 commit 7106d6b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/firejail/restrict_users.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static void sanitize_passwd(void) {
// process uid
int uid;
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 @@ -351,7 +351,7 @@ static void sanitize_group(void) {
// process uid
int gid;
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

0 comments on commit 7106d6b

Please sign in to comment.