acl-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] libacl: use getpwnam_r and getgrnam_r in acl_from_text.c


From: lzaoral
Subject: [PATCH] libacl: use getpwnam_r and getgrnam_r in acl_from_text.c
Date: Wed, 20 Jul 2022 15:33:30 +0200

From: Lukáš Zaoral <lzaoral@redhat.com>

Plain getpwnam and getgrnam are not thread safe.

Fixes: #62371
---
 libacl/acl_from_text.c | 50 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 8 deletions(-)

diff --git a/libacl/acl_from_text.c b/libacl/acl_from_text.c
index 2617cbb..b46e694 100644
--- a/libacl/acl_from_text.c
+++ b/libacl/acl_from_text.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <pwd.h>
 #include <grp.h>
+#include <unistd.h>
 #include "libacl.h"
 #include "misc.h"
 
@@ -146,21 +147,45 @@ get_id(const char *token, id_t *id_p)
 }
 
 
+static int
+allocate_buffer(char **buffer, long *bufsize, int type)
+{
+       *bufsize = sysconf(type);
+       if (*bufsize == -1)
+               *bufsize = 16384;
+
+       *buffer = malloc(*bufsize);
+       if (*buffer == NULL)
+               return -1;
+
+       return 0;
+}
+
+
 static int
 get_uid(const char *token, uid_t *uid_p)
 {
-       struct passwd *passwd;
+       struct passwd passwd;
+       struct passwd *result;
+       char * buffer;
+       long bufsize;
 
        if (get_id(token, uid_p) == 0)
                return 0;
+
+       if (allocate_buffer(&buffer, &bufsize, _SC_GETPW_R_SIZE_MAX) != 0)
+               return -1;
+
        errno = 0;
-       passwd = getpwnam(token);
-       if (passwd) {
-               *uid_p = passwd->pw_uid;
+       getpwnam_r(token, &passwd, buffer, bufsize, &result);
+       if (result) {
+               *uid_p = passwd.pw_uid;
+               free(buffer);
                return 0;
        }
        if (errno == 0)
                errno = EINVAL;
+       free(buffer);
        return -1;
 }
 
@@ -168,18 +193,27 @@ get_uid(const char *token, uid_t *uid_p)
 static int
 get_gid(const char *token, gid_t *gid_p)
 {
-       struct group *group;
+       struct group group;
+       struct group *result;
+       char * buffer;
+       long bufsize;
 
        if (get_id(token, (uid_t *)gid_p) == 0)
                return 0;
+
+       if (allocate_buffer(&buffer, &bufsize, _SC_GETGR_R_SIZE_MAX) != 0)
+               return -1;
+
        errno = 0;
-       group = getgrnam(token);
-       if (group) {
-               *gid_p = group->gr_gid;
+       getgrnam_r(token, &group, buffer, bufsize, &result);
+       if (result) {
+               *gid_p = group.gr_gid;
+               free(buffer);
                return 0;
        }
        if (errno == 0)
                errno = EINVAL;
+       free(buffer);
        return -1;
 }
 
-- 
2.36.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]