Skip to contentSkip to conversation

Do not save pointer to 'struct passwd' returned by getpwnam etc.

PATCH In review [email protected]... 2 participants 6 messages Mar 6, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Do not save pointer to 'struct passwd' returned by getpwnam etc.

From: Selva Nair <[email protected]...>

- This pointer is to a static area which can change on further calls to getpwnam, getpwuid etc.
Same with struct group returned by getgrnam.

As the only field later referred to is uid or gid, fix by saving them instead.

---
Though we call getpwnam()/getgrnam() more than once with potentially different user/group names, this has not yet led to a bug because only in one place the state is persisted, and that call happens later. But looks like a bug waiting to happen.

 src/openvpn/platform.c | 36 +++++++++++++++++++++++-------------
 src/openvpn/platform.h | 14 ++++----------
 src/openvpn/tun.c      |  4 ++--
 3 files changed, 29 insertions(+), 25 deletions(-)
src/openvpn/platform.c +23 -13
index 580c4cb8..f6b856f3 100644
@@ -85,11 +85,16 @@ platform_user_get(const char *username, struct platform_state_user *state)
     if (username)
     {
 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
-        state->pw = getpwnam(username);
-        if (!state->pw)
+        state->uid = -1;
+        const struct passwd *pw = getpwnam(username);
+        if (!pw)
         {
             msg(M_ERR, "failed to find UID for user %s", username);
         }
+        else
+        {
+            state->uid = pw->pw_uid;
+        }
         state->username = username;
         ret = true;
 #else  /* if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) */
@@ -103,9 +108,9 @@ static void
 platform_user_set(const struct platform_state_user *state)
 {
 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
-    if (state->username && state->pw)
+    if (state->username && state->uid >= 0)
     {
-        if (setuid(state->pw->pw_uid))
+        if (setuid(state->uid))
         {
             msg(M_ERR, "setuid('%s') failed", state->username);
         }
@@ -124,11 +129,16 @@ platform_group_get(const char *groupname, struct platform_state_group *state)
     if (groupname)
     {
 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
-        state->gr = getgrnam(groupname);
-        if (!state->gr)
+        state->gid = -1;
+        const struct group *gr = getgrnam(groupname);
+        if (!gr)
         {
             msg(M_ERR, "failed to find GID for group %s", groupname);
         }
+        else
+        {
+            state->gid = gr->gr_gid;
+        }
         state->groupname = groupname;
         ret = true;
 #else  /* if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) */
@@ -142,9 +152,9 @@ static void
 platform_group_set(const struct platform_state_group *state)
 {
 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
-    if (state->groupname && state->gr)
+    if (state->groupname && state->gid >= 0)
     {
-        if (setgid(state->gr->gr_gid))
+        if (setgid(state->gid))
         {
             msg(M_ERR, "setgid('%s') failed", state->groupname);
         }
@@ -152,7 +162,7 @@ platform_group_set(const struct platform_state_group *state)
 #ifdef HAVE_SETGROUPS
         {
             gid_t gr_list[1];
-            gr_list[0] = state->gr->gr_gid;
+            gr_list[0] = state->gid;
             if (setgroups(1, gr_list))
             {
                 msg(M_ERR, "setgroups('%s') failed", state->groupname);
@@ -225,13 +235,13 @@ platform_user_group_set(const struct platform_state_user *user_state,
      * new_uid/new_gid defaults to -1, which will not make
      * libcap-ng change the UID/GID unless configured
      */
-    if (group_state->groupname && group_state->gr)
+    if (group_state->groupname && group_state->gid >= 0)
     {
-        new_gid = group_state->gr->gr_gid;
+        new_gid = group_state->gid;
     }
-    if (user_state->username && user_state->pw)
+    if (user_state->username && user_state->uid >= 0)
     {
-        new_uid = user_state->pw->pw_uid;
+        new_uid = user_state->uid;
     }
 
     /* Prepare capabilities before dropping UID/GID */
src/openvpn/platform.h +4 -10
index a35a571c..d8dad74b 100644
@@ -63,7 +63,7 @@ struct context;
 struct platform_state_user {
 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
     const char *username;
-    struct passwd *pw;
+    uid_t uid;
 #else
     int dummy;
 #endif
@@ -74,7 +74,7 @@ struct platform_state_user {
 struct platform_state_group {
 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
     const char *groupname;
-    struct group *gr;
+    gid_t gid;
 #else
     int dummy;
 #endif
@@ -97,10 +97,7 @@ static inline int
 platform_state_user_uid(const struct platform_state_user *s)
 {
 #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
-    if (s->pw)
-    {
-        return s->pw->pw_uid;
-    }
+    return s->uid;
 #endif
     return -1;
 }
@@ -109,10 +106,7 @@ static inline int
 platform_state_group_gid(const struct platform_state_group *s)
 {
 #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
-    if (s->gr)
-    {
-        return s->gr->gr_gid;
-    }
+    return s->gid;
 #endif
     return -1;
 }
src/openvpn/tun.c +2 -2
index a1414d23..87033277 100644
@@ -2242,7 +2242,7 @@ tuncfg(const char *dev, const char *dev_type, const char *dev_node,
         {
             msg(M_ERR, "Cannot get user entry for %s", username);
         }
-        else if (ioctl(tt->fd, TUNSETOWNER, platform_state_user.pw->pw_uid) < 0)
+        else if (ioctl(tt->fd, TUNSETOWNER, platform_state_user.uid) < 0)
         {
             msg(M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
         }
@@ -2255,7 +2255,7 @@ tuncfg(const char *dev, const char *dev_type, const char *dev_node,
         {
             msg(M_ERR, "Cannot get group entry for %s", groupname);
         }
-        else if (ioctl(tt->fd, TUNSETGROUP, platform_state_group.gr->gr_gid) < 0)
+        else if (ioctl(tt->fd, TUNSETGROUP, platform_state_group.gid) < 0)
         {
             msg(M_ERR, "Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev);
         }
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 Include supplementary groups when checking management-client-group

From: Selva Nair <[email protected]...>

- When management-client-group is in use, allow access if any of the supplementary groups of the user matches the specified group.

Currently only the effective gid of the peer socket is checked which is normally the primary group of user. As unprivileged users have no easy way of changing the effective gid of a process, group based access control is of very limited use without this change.

- Also accept if uid = 0 irrespective of the group.

Github: OpenVPN/openvpn#264

---
 configure.ac         |  2 +-
 src/openvpn/manage.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 2 deletions(-)
configure.ac +1 -1
index 67f680b2..979903a8 100644
@@ -659,7 +659,7 @@ AC_CHECK_FUNCS([ \
 	daemon chroot getpwnam setuid nice system dup dup2 \
 	syslog openlog mlockall getrlimit getgrnam setgid \
 	setgroups flock readv writev time gettimeofday \
-	setsid chdir \
+	setsid chdir getpwuid getgrent \
 	chsize ftruncate execve getpeereid basename dirname access \
 	epoll_create strsep \
 ])
src/openvpn/manage.c +42 -1

61 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Include supplementary groups when checking management-client-group

Hi,

Quoted 9 lines · On Mon, Mar 06, 2023 at 12:33:46AM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- When management-client-group is in use, allow access if any of the supplementary groups of the user matches the specified group.

Currently only the effective gid of the peer socket is checked which is normally the primary group of user. As unprivileged users have no easy way of changing the effective gid of a process, group based access control is of very limited use without this change.

I'm not really convinced this is a good way forward - it's yet another extra function for a very niche use case, as in "the code has been like this for 100 years, and nobody has ever used it".

What you do is also not exactly "the user connecting has this gid at the time of connection" but "the uid reported by getpeerid() has this group listed in /etc/groups", which will be the same in many cases, but is still checking something different.

I would propose to extend the documentation with "this only checks the primary gid at time of connection, if this does not work for your use case, put the socket into a directory with the right gid and mode 750 and do not use that openvpn option" - as you wrote in the github ticket.

Quoted 1 line

- Also accept if uid = 0 irrespective of the group.

This part is fine for me, as it follows the traditional unix way of "root may pass".

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
author
#3 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Include supplementary groups when checking management-client-group

Hi,

Quoted 13 lines · Gert Doering

Hi,

Quoted 9 lines · On Mon, Mar 06, 2023 at 12:33:46AM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- When management-client-group is in use, allow access if any of the supplementary groups of the user matches the specified group.

Currently only the effective gid of the peer socket is checked which is normally the primary group of user. As unprivileged users have no easy way of changing the effective gid of a process, group based access control is of very limited use without this change.

Quoted 9 lines

I'm not really convinced this is a good way forward - it's yet another extra function for a very niche use case, as in "the code has been like this for 100 years, and nobody has ever used it".

What you do is also not exactly "the user connecting has this gid at the time of connection" but "the uid reported by getpeerid() has this group listed in /etc/groups", which will be the same in many cases, but is still checking something different.

I was of the same opinion when I first saw issue #264, but I think the original
intent of "--management-client-group has to be what I've implemented or something similar.

Docs refer to it as group (not effective gid of the socket), and restricting based on
gid reported by getpeerid is kind of useless. Why use "group" if it can't be used
to restrict access to a set of users using a supplementary group...

I would propose to extend the documentation with "this only checks the

Quoted 4 lines

primary gid at time of connection, if this does not work for your use case, put the socket into a directory with the right gid and mode 750 and do not use that openvpn option" - as you wrote in the github ticket.

It's impossible to save that option by documenting. In that case, let's just
make it a no-op and document that this option has no effect. And suggest the above for how to achieve group-based access control..

Quoted 5 lines
Quoted 1 line

- Also accept if uid = 0 irrespective of the group.

This part is fine for me, as it follows the traditional unix way of "root may pass".

I'm not keen on touching some code and ignore that a closely related code stays crippled (IMO). Unless we can make --management-client-group a no-op as mentioned above.

As you say this is an obscure option -- we can leave it "as is".

Selva

Acked-by on v1 1/2
#5 lore raw
In reply to [email protected]...

Well spotted. Whatever we decide to do with 2/2, this is the correct way to deal with getpw*() returns.

I have not tested every possible combination of options, but I have stared-at-code (looks good), had the buildbots test it, and ran a "--user nobody" DCO instance on Linux (works).

Your patch has been applied to the master and release/2.6 branch.

commit 62024046dffd6ff10309b791cd6600fe80bc46e3 (master) commit 85ad9d2520d571dff93d2b15002f151c10000804 (release/2.6)

Author: Selva Nair
Date:   Mon Mar 6 00:33:45 2023 -0500
     Do not save pointer to 'struct passwd' returned by getpwnam etc.

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26332.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

author
#4 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Include supplementary groups when checking management-client-group

In reply to [email protected]...

Hi

FTR, I just noticed that the patch is missing an endgrent() call:

Quoted 8 lines · On Mon, Mar 6, 2023 at 12:33 AM <[email protected]...> wrote:
src/openvpn/manage.c +6 -0
+    struct group *gr = getgrent();
+    char **members = NULL;
+    while (gr)
+    {
+        if (gr->gr_gid == gid)
+        {

src/openvpn/manage.c man_new_connection_post(struct management *man, const char *description) · line 1805

Quoted 10 lines
src/openvpn/manage.c +10 -0
+            /* found the group -- check user is a member */
+            members = gr->gr_mem;
+            for (char *s = *members; s && !ret; s++)
+            {
+                ret = !strcmp(s, pw->pw_name);
+            }
+            break; /* out of the while loop */
+        }
+        gr = getgrent();
+    }
          endgrent();

src/openvpn/manage.c man_new_connection_post(struct management *man, const char *description) · line 1809

Quoted 5 lines
src/openvpn/manage.c +4 -0
+#endif /* if defined(HAVE_GETPWUID) && defined(HAVE_GETGRENT) */
+    return ret;
+}
+

Will delay v2 depending on the fate of this patch.

Selva