From 7106d6b38d465fc0a7a7f69cad4a3f9dd5cd8d22 Mon Sep 17 00:00:00 2001 From: "Kelvin M. Klann" Date: Wed, 31 Jan 2024 21:23:55 -0300 Subject: [PATCH] bugfix: 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 4f003daec ("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 --- src/firejail/restrict_users.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/firejail/restrict_users.c b/src/firejail/restrict_users.c index 741e908edc4..261c73ac586 100644 --- a/src/firejail/restrict_users.c +++ b/src/firejail/restrict_users.c @@ -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 @@ -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