From: "cron2 (Code Review)" Date: Sun, 5 Oct 2025 20:42:02 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: <16ed8382eabf91b899a071f5ecb68975a2f771dd-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="wb86tySMmfM="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: platform: Do not assume uid_t/gid_t are signed List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: MaxF , plaisthos , openvpn-devel --wb86tySMmfM= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/12= 06?usp=3Demail ) Change subject: platform: Do not assume uid_t/gid_t are signed ...................................................................... platform: Do not assume uid_t/gid_t are signed uid_t/gid_t are int on many platform but unsigned on at least Linux. So rewrite the code in a way that does not make any assumptions about the types. Mainly this means storing the information whether the value is valid in a separate bool and not in the value itself. Note that this changes the return behavior of platform_{user,group}_get but a review of the callers determined that this makes no actual difference. Change-Id: Ie6b4c41d13544d5ba71d441cc794c7abd12408f3 Signed-off-by: Frank Lichtenheld Acked-by: MaxF Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1206 Message-Id: <20251003100602.375062-1-frank@...2641...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg33= 266.html Signed-off-by: Gert Doering --- M src/openvpn/manage.c M src/openvpn/manage.h M src/openvpn/platform.c M src/openvpn/platform.h M src/openvpn/socket.c M src/openvpn/socket.h 6 files changed, 29 insertions(+), 64 deletions(-) diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c index 5a41a0f..1cb5c63 100644 --- a/src/openvpn/manage.c +++ b/src/openvpn/manage.c @@ -1847,25 +1847,26 @@ static bool man_verify_unix_peer_uid_gid(struct management *man, const socket_descript= or_t sd) { - if (socket_defined(sd) && (man->settings.client_uid !=3D -1 || man->se= ttings.client_gid !=3D -1)) + if (socket_defined(sd) && (man->settings.user.user_valid || man->setti= ngs.group.group_valid)) { static const char err_prefix[] =3D "MANAGEMENT: unix domain socket client connection rejected --"; - int uid, gid; + uid_t uid; + gid_t gid; if (unix_socket_get_peer_uid_gid(man->connection.sd_cli, &uid, &gi= d)) { - if (man->settings.client_uid !=3D -1 && man->settings.client_u= id !=3D uid) + if (man->settings.user.user_valid && man->settings.user.uid != =3D uid) { msg(D_MANAGEMENT, "%s UID of socket peer (%d) doesn't match required val= ue (%d) as given by --management-client-user", - err_prefix, uid, man->settings.client_uid); + err_prefix, uid, man->settings.user.uid); return false; } - if (man->settings.client_gid !=3D -1 && man->settings.client_g= id !=3D gid) + if (man->settings.group.group_valid && man->settings.group.gid= !=3D gid) { msg(D_MANAGEMENT, "%s GID of socket peer (%d) doesn't match required val= ue (%d) as given by --management-client-group", - err_prefix, gid, man->settings.client_gid); + err_prefix, gid, man->settings.group.gid); return false; } } @@ -2542,8 +2543,6 @@ CLEAR(*ms); ms->flags =3D flags; - ms->client_uid =3D -1; - ms->client_gid =3D -1; /* * Get username/password @@ -2553,27 +2552,21 @@ get_user_pass(&ms->up, pass_file, "Management", GET_USER_PASS_= PASSWORD_ONLY); } +#if UNIX_SOCK_SUPPORT /* * lookup client UID/GID if specified */ if (client_user) { - struct platform_state_user s; - platform_user_get(client_user, &s); - ms->client_uid =3D platform_state_user_uid(&s); - msg(D_MANAGEMENT, "MANAGEMENT: client_uid=3D%d", ms->client_ui= d); - ASSERT(ms->client_uid >=3D 0); + ASSERT(platform_user_get(client_user, &ms->user)); + msg(D_MANAGEMENT, "MANAGEMENT: client_uid=3D%d", ms->user.uid); } if (client_group) { - struct platform_state_group s; - platform_group_get(client_group, &s); - ms->client_gid =3D platform_state_group_gid(&s); - msg(D_MANAGEMENT, "MANAGEMENT: client_gid=3D%d", ms->client_gi= d); - ASSERT(ms->client_gid >=3D 0); + ASSERT(platform_group_get(client_group, &ms->group)); + msg(D_MANAGEMENT, "MANAGEMENT: client_gid=3D%d", ms->group.gid= ); } -#if UNIX_SOCK_SUPPORT if (ms->flags & MF_UNIX_SOCK) { sockaddr_unix_init(&ms->local_unix, addr); diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h index bff96d3..a31eb06 100644 --- a/src/openvpn/manage.h +++ b/src/openvpn/manage.h @@ -242,14 +242,14 @@ struct addrinfo *local; #if UNIX_SOCK_SUPPORT struct sockaddr_un local_unix; + struct platform_state_user user; + struct platform_state_group group; #endif bool management_over_tunnel; struct user_pass up; int log_history_cache; int echo_buffer_size; int state_buffer_size; - int client_uid; - int client_gid; /* flags for handling the management interface "signal" command */ #define MANSIG_IGNORE_USR1_HUP (1u << 0) diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index 880d14e..a1ffdaf 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -39,7 +39,7 @@ #include "platform.h" -#if _WIN32 +#ifdef _WIN32 #include #endif @@ -79,12 +79,10 @@ bool platform_user_get(const char *username, struct platform_state_user *state) { - bool ret =3D false; CLEAR(*state); if (username) { #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) - state->uid =3D -1; const struct passwd *pw =3D getpwnam(username); if (!pw) { @@ -93,23 +91,23 @@ else { state->uid =3D pw->pw_uid; + state->user_valid =3D true; } state->username =3D username; - ret =3D true; #else /* if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) */ msg(M_FATAL, "cannot get UID for user %s -- platform lacks getpwname() or s= etuid() system calls", username); #endif } - return ret; + return state->user_valid; } static void platform_user_set(const struct platform_state_user *state) { #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) - if (state->username && state->uid >=3D 0) + if (state->username && state->user_valid) { if (setuid(state->uid)) { @@ -125,12 +123,10 @@ bool platform_group_get(const char *groupname, struct platform_state_group *sta= te) { - bool ret =3D false; CLEAR(*state); if (groupname) { #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) - state->gid =3D -1; const struct group *gr =3D getgrnam(groupname); if (!gr) { @@ -139,23 +135,23 @@ else { state->gid =3D gr->gr_gid; + state->group_valid =3D true; } state->groupname =3D groupname; - ret =3D true; #else /* if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) */ msg(M_FATAL, "cannot get GID for group %s -- platform lacks getgrnam() or s= etgid() system calls", groupname); #endif } - return ret; + return state->group_valid; } static void platform_group_set(const struct platform_state_group *state) { #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) - if (state->groupname && state->gid >=3D 0) + if (state->groupname && state->group_valid) { if (setgid(state->gid)) { @@ -237,13 +233,13 @@ * 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->gid >=3D 0) + if (group_state->groupname && group_state->group_valid) { - new_gid =3D group_state->gid; + new_gid =3D (int)group_state->gid; } - if (user_state->username && user_state->uid >=3D 0) + if (user_state->username && user_state->user_valid) { - new_uid =3D user_state->uid; + new_uid =3D (int)user_state->uid; } /* Prepare capabilities before dropping UID/GID */ diff --git a/src/openvpn/platform.h b/src/openvpn/platform.h index f1a2b01..0cb25f5 100644 --- a/src/openvpn/platform.h +++ b/src/openvpn/platform.h @@ -64,9 +64,8 @@ #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) const char *username; uid_t uid; -#else - int dummy; #endif + bool user_valid; }; /* Get/Set GID of process */ @@ -76,9 +75,8 @@ #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) const char *groupname; gid_t gid; -#else - int dummy; #endif + bool group_valid; }; bool platform_user_get(const char *username, struct platform_state_user *s= tate); @@ -89,28 +87,6 @@ const struct platform_state_group *group_stat= e, struct context *c); -/* - * Extract UID or GID - */ - -static inline int -platform_state_user_uid(const struct platform_state_user *s) -{ -#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) - return s->uid; -#endif - return -1; -} - -static inline int -platform_state_group_gid(const struct platform_state_group *s) -{ -#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) - return s->gid; -#endif - return -1; -} - void platform_chroot(const char *path); void platform_nice(int niceval); diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 5fcf820..f71d891 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -3082,7 +3082,7 @@ } bool -unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, int *uid, int *= gid) +unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid, gid= _t *gid) { #ifdef HAVE_GETPEEREID uid_t u; diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index e45981f..2c79a11 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -371,7 +371,7 @@ void socket_delete_unix(const struct sockaddr_un *local); -bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, int *uid, = int *gid); +bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid= , gid_t *gid); #endif /* if UNIX_SOCK_SUPPORT */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1206?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ie6b4c41d13544d5ba71d441cc794c7abd12408f3 Gerrit-Change-Number: 1206 Gerrit-PatchSet: 3 Gerrit-Owner: flichtenheld Gerrit-Reviewer: MaxF Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --wb86tySMmfM= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

platform: Do not assume uid_t/gid_t are signed
uid_t/gid_t are int on many platform but unsigned
on at least Linu= x. So rewrite the code in a way that
does not make any assumptions about= the types. Mainly
this means storing the information whether the value<= br>is valid in a separate bool and not in the value
itself.

Note = that this changes the return behavior of
platform_{user,group}_get but a= review of the
callers determined that this makes no actual
differenc= e.

Change-Id: Ie6b4c41d13544d5ba71d441cc794c7abd12408f3
Signed-of= f-by: Frank Lichtenheld <frank@...2641...>
Acked-by: MaxF <max@= ...2606...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1206Message-Id: <20251003100602.375062-1-frank@...2641...>
URL: http= s://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg33266.html<= br>Signed-off-by: Gert Doering <gert@...1296...>
---
M src/open= vpn/manage.c
M src/openvpn/manage.h
M src/openvpn/platform.c
M src= /openvpn/platform.h
M src/openvpn/socket.c
M src/openvpn/socket.h
= 6 files changed, 29 insertions(+), 64 deletions(-)

diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c
index 5a41a0f..1cb5c63 100644
--- a/src/openvpn/manage.c
+++ b/src/openvpn/manage.c
@@ -1847,25 +1847= ,26 @@
static bool
man_verify_unix_peer_u= id_gid(struct management *man, const socket_descriptor_t sd)
{

- if (socket_de= fined(sd) && (man->settings.client_uid !=3D -1 || man->settin= gs.client_gid !=3D -1))
+ if (socket_defined(sd) && (man->settings.user.user_valid= || man->settings.group.group_valid))
{
<= span> static const char err_prefix[] =3D
= "MANAGEMENT: unix domain socket client connection rejected --"= ;
- int uid, gid= ;
+ uid_t uid;=
+ gid_t gid;<= /span>
if (unix_socket_get_peer_uid_gid(man->connectio= n.sd_cli, &uid, &gid))
{
- if (man->settings.client= _uid !=3D -1 && man->settings.client_uid !=3D uid)
+ if (man->settings.= user.user_valid && man->settings.user.uid !=3D uid)

{
msg(D_MANAGEMENT,

"%s UID of socket peer (%d) doesn'= t match required value (%d) as given by --management-client-user",

- err_p= refix, uid, man->settings.client_uid);
+ err_prefix, uid, man->settings= .user.uid);
return false;
= }
- = if (man->settings.client_gid !=3D -1 && man->settings.cl= ient_gid !=3D gid)
+ = if (man->settings.group.group_valid && man->settin= gs.group.gid !=3D gid)
{
= msg(D_MANAGEMENT,
"%s= GID of socket peer (%d) doesn't match required value (%d) as given by = --management-client-group",
- err_prefix, gid, man->settings.client_gid= );
+ = err_prefix, gid, man->settings.group.gid);
= return false;
}
= }
@@ -2542,8 +2543,6 @@
CLEAR(*m= s);

ms->flags =3D flags;
- ms->client_uid = =3D -1;
- ms->= ;client_gid =3D -1;

/* * Get username/password
@@ -2553,27 +2552,= 21 @@
get_user_pass(&ms->up, pass_file,= "Management", GET_USER_PASS_PASSWORD_ONLY);
= }

+#if UNIX_SOCK_SUPPORT
/*
= * lookup client UID/GID if specified
*/
if (client_user)
{- struct platform_sta= te_user s;
- = platform_user_get(client_user, &s);
- ms->client_uid =3D platform_state_user_ui= d(&s);
- = msg(D_MANAGEMENT, "MANAGEMENT: client_uid=3D%d", ms->client_u= id);
- ASSER= T(ms->client_uid >=3D 0);
+ ASSERT(platform_user_get(client_user, &ms->u= ser));
+ m= sg(D_MANAGEMENT, "MANAGEMENT: client_uid=3D%d", ms->user.uid);=
}
if (client_group)
{
= - struct platform_state_group s;
- platform_group_get(client_group, &s);=
- ms->cl= ient_gid =3D platform_state_group_gid(&s);
- msg(D_MANAGEMENT, "MANAGEMENT: cl= ient_gid=3D%d", ms->client_gid);
- ASSERT(ms->client_gid >=3D 0);<= br>+ ASSERT(platform= _group_get(client_group, &ms->group));
+ msg(D_MANAGEMENT, "MANAGEMENT: c= lient_gid=3D%d", ms->group.gid);
}<= br>
-#if UNIX_SO= CK_SUPPORT
if (ms->flags & MF_UNIX_SOCK)
{
sockaddr_unix_init(&a= mp;ms->local_unix, addr);
diff --git a/src/openvpn/manag= e.h b/src/openvpn/manage.h
index bff96d3..a31eb06 100644
--- a/src/openvpn/manage.h
+++ b/src/openvpn/= manage.h
@@ -242,14 +242,14 @@
struct = addrinfo *local;
#if UNIX_SOCK_SUPPORT
= struct sockaddr_un local_unix;
+ struct platform_state_user user;
+ struct platform_state_group group;=
#endif
bool management_over_tunnel;<= br> struct user_pass up;
int log_history_cac= he;
int echo_buffer_size;
int sta= te_buffer_size;
- in= t client_uid;
- int = client_gid;

/* flags for handling the ma= nagement interface "signal" command */
#define M= ANSIG_IGNORE_USR1_HUP (1u << 0)
diff --git a/src/ope= nvpn/platform.c b/src/openvpn/platform.c
index 880d14e..a1f= fdaf 100644
--- a/src/openvpn/platform.c
++= + b/src/openvpn/platform.c
@@ -39,7 +39,7 @@

#include "platform.h"

-#if _WIN32
+#ifdef _WIN32

#inc= lude <direct.h>
#endif

<= span>@@ -79,12 +79,10 @@

bool
platform_us= er_get(const char *username, struct platform_state_user *state)
<= span> {

- bool ret = =3D false;
CLEAR(*state);
if (use= rname)
{
#if defined(HAVE_GETPWNAM) &= amp;& defined(HAVE_SETUID)
- state->uid =3D -1;
const struc= t passwd *pw =3D getpwnam(username);
if (!pw)
{
@@ -93,23 +91,23 @@
= else
{
state= ->uid =3D pw->pw_uid;
+ state->user_valid =3D true;
= }
state->username =3D username;
- ret =3D true;

#else /* if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) */
msg(M_FATAL,
"cann= ot get UID for user %s -- platform lacks getpwname() or setuid() system cal= ls",
username);
#endif
}
-= return ret;
+ = return state->user_valid;
}
static void
platform_user_set(const struct platfor= m_state_user *state)
{
#if defined(HAVE_G= ETPWNAM) && defined(HAVE_SETUID)
- if (state->username && state->uid >= =3D 0)
+ if (state= ->username && state->user_valid)
{
if (setuid(state->uid))
{
@@ -125,12 +123,10 @@
bool
= platform_group_get(const char *groupname, struct platform_state_group *sta= te)
{
= - bool ret =3D false;
CLEAR(*state);
if (groupname)

{
#if defined(H= AVE_GETGRNAM) && defined(HAVE_SETGID)
- state->gid =3D -1;
= const struct group *gr =3D getgrnam(groupname);
= if (!gr)
{
@@ -139,23 +135,23 @@=
else
{
= state->gid =3D gr->gr_gid;
+ state->group_valid =3D true;=
}
state->groupname =3D group= name;
- ret =3D = true;
#else /* if defined(HAVE_GETGRNAM) && define= d(HAVE_SETGID) */
msg(M_FATAL,
= "cannot get GID for group %s -- platform lacks getgrnam() or= setgid() system calls",
groupname);

#endif
}
- return ret;
+ return state->group_valid;
}

static void
platform_group_s= et(const struct platform_state_group *state)
{
<= span> #if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)

= - if (state->groupname &= ;& state->gid >=3D 0)
+ if (state->groupname && state->group_valid)
{
if (setgid(state->gid))
{
@@ -237,13 +233,13 @@
<= span> * new_uid/new_gid defaults to -1, which will not make

= * libcap-ng change the UID/GID unless configured
*/

- if (gro= up_state->groupname && group_state->gid >=3D 0)
= + if (group_state->groupn= ame && group_state->group_valid)
{- new_gid =3D group_stat= e->gid;
+ n= ew_gid =3D (int)group_state->gid;
}
- if (user_state->username &= & user_state->uid >=3D 0)

+ if (user_state->username && user_state->u= ser_valid)
{
- new_uid =3D user_state->uid;
+ new_uid =3D (int)user_state->u= id;
}

/* Prepar= e capabilities before dropping UID/GID */
diff --git a/src/= openvpn/platform.h b/src/openvpn/platform.h
index f1a2b01..= 0cb25f5 100644
--- a/src/openvpn/platform.h
+++ b/src/openvpn/platform.h

@@ -64,9 +64,8 @@
<= span> #if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
= const char *username;
uid_t uid;
= -#else
- int dummy;
#endif+ bool user_valid; };

/* Get/Set GID of process */=
@@ -76,9 +75,8 @@
#if defined(HAVE_GETGRN= AM) && defined(HAVE_SETGID)
const char *groupn= ame;
gid_t gid;
-#else
- = int dummy;
#endif
+ bool group_valid;
};

bool platform_user_get(const char *username, struct pla= tform_state_user *state);
@@ -89,28 +87,6 @@
const struct platform_state_group *group_st= ate, struct context *c);


-/*
- * Extract UID or GID
- */
-
-static inline int
-platform_state_user_uid= (const struct platform_state_user *s)
-{
-#if = defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
- return s->uid;
-#endif
- return -1;
-}
--static inline int
-platform_state_group_gid(const str= uct platform_state_group *s)

-{
-#if defined(H= AVE_GETGRNAM) && defined(HAVE_SETGID)
- return s->gid;
-#endif
- return -1;
-}
-
= void platform_chroot(const char *path);

= void platform_nice(int niceval);
diff --git a/src/openvpn/s= ocket.c b/src/openvpn/socket.c
index 5fcf820..f71d891 10064= 4
--- a/src/openvpn/socket.c
+++ b/src/open= vpn/socket.c
@@ -3082,7 +3082,7 @@
}


bool
-unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, int= *uid, int *gid)
+uni= x_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid, gid_t *= gid)
{
#ifdef HAVE_GETPEEREID
<= span> uid_t u;
diff --git a/src/openvpn/socket.h b/src/= openvpn/socket.h
index e45981f..2c79a11 100644
--- a/src/openvpn/socket.h
+++ b/src/openvpn/socket.h
@@ -371,7 +371,7 @@

void = socket_delete_unix(const struct sockaddr_un *local);

-bool unix_socket_get_peer_= uid_gid(const socket_descriptor_t sd, int *uid, int *gid);
+bool unix_socket_get_peer_uid_gid(co= nst socket_descriptor_t sd, uid_t *uid, gid_t *gid);

#endif /* if UNIX_SOCK_SUPPORT */

=

To view, visit change 1206. To unsubscribe, or for he= lp writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ie6b4c41d13544d5ba71d441cc79= 4c7abd12408f3
Gerrit-Change-Number: 1206
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: MaxF <max@...2606...> <= /div>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --wb86tySMmfM=--