]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/user-util: avoid filesystem access check
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 19 Aug 2022 14:45:08 +0000 (16:45 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 24 Aug 2022 08:02:46 +0000 (10:02 +0200)
The check of u==UID_NOBODY is just a register comparison, but
synthesize_nobody() requires a system call, so let's invert the order in the
condition. Since most calls into this module are not for nobody, we should
save one syscall in the common case.

src/basic/user-util.c

index 37ccb667b27f67931b1ceaad237339963fa7d0dd..e705f703eada1f4c66962d9374779a6b3c8ebc58 100644 (file)
@@ -196,8 +196,8 @@ static int synthesize_user_creds(
                 return 0;
         }
 
-        if (synthesize_nobody() &&
-            STR_IN_SET(*username, NOBODY_USER_NAME, "65534")) {
+        if (STR_IN_SET(*username, NOBODY_USER_NAME, "65534") &&
+            synthesize_nobody()) {
                 *username = NOBODY_USER_NAME;
 
                 if (uid)
@@ -341,8 +341,8 @@ int get_group_creds(const char **groupname, gid_t *gid, UserCredsFlags flags) {
                 return 0;
         }
 
-        if (synthesize_nobody() &&
-            STR_IN_SET(*groupname, NOBODY_GROUP_NAME, "65534")) {
+        if (STR_IN_SET(*groupname, NOBODY_GROUP_NAME, "65534") &&
+            synthesize_nobody()) {
                 *groupname = NOBODY_GROUP_NAME;
 
                 if (gid)
@@ -388,8 +388,7 @@ char* uid_to_name(uid_t uid) {
         /* Shortcut things to avoid NSS lookups */
         if (uid == 0)
                 return strdup("root");
-        if (synthesize_nobody() &&
-            uid == UID_NOBODY)
+        if (uid == UID_NOBODY && synthesize_nobody())
                 return strdup(NOBODY_USER_NAME);
 
         if (uid_is_valid(uid)) {
@@ -432,8 +431,7 @@ char* gid_to_name(gid_t gid) {
 
         if (gid == 0)
                 return strdup("root");
-        if (synthesize_nobody() &&
-            gid == GID_NOBODY)
+        if (gid == GID_NOBODY && synthesize_nobody())
                 return strdup(NOBODY_GROUP_NAME);
 
         if (gid_is_valid(gid)) {
@@ -600,8 +598,8 @@ int get_home_dir(char **_h) {
                 *_h = h;
                 return 0;
         }
-        if (synthesize_nobody() &&
-            u == UID_NOBODY) {
+
+        if (u == UID_NOBODY && synthesize_nobody()) {
                 h = strdup("/");
                 if (!h)
                         return -ENOMEM;
@@ -657,8 +655,7 @@ int get_shell(char **_s) {
                 *_s = s;
                 return 0;
         }
-        if (synthesize_nobody() &&
-            u == UID_NOBODY) {
+        if (u == UID_NOBODY && synthesize_nobody()) {
                 s = strdup(NOLOGIN);
                 if (!s)
                         return -ENOMEM;