Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 1ca70ae

Browse files
authored
Merge pull request #259 from amshinde/handle-numeric-gids-upstream
Handle additional groups that may not exist in the container.
2 parents 26c6654 + df9d326 commit 1ca70ae

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ AM_PROG_CC_C_O
1414
# Checks for libraries.
1515

1616
# Checks for header files.
17-
AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h stddef.h stdint.h stdlib.h string.h sys/mount.h sys/socket.h unistd.h],
17+
AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h stddef.h stdint.h stdlib.h string.h sys/mount.h sys/socket.h unistd.h stdbool.h],
1818
[headers_found=yes],
1919
[headers_found=no])
2020

src/exec.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,18 @@ static int hyper_setup_exec_user(struct hyper_exec *exec)
267267
goto fail;
268268
groups = reallocgroups;
269269
for (i = 0; i < exec->nr_additional_groups; i++) {
270+
unsigned long id;
270271
fprintf(stdout, "try to find the group: %s\n", exec->additional_groups[i]);
271-
struct group *gr = hyper_getgrnam(exec->additional_groups[i]);
272-
if (gr == NULL) {
273-
perror("can't find the group");
274-
goto fail;
272+
if (hyper_name_to_id(exec->additional_groups[i], &id)) {
273+
groups[ngroups] = id;
274+
} else {
275+
struct group *gr = hyper_getgrnam(exec->additional_groups[i]);
276+
if (gr == NULL) {
277+
perror("can't find the group");
278+
goto fail;
279+
}
280+
groups[ngroups] = gr->gr_gid;
275281
}
276-
groups[ngroups] = gr->gr_gid;
277282
ngroups++;
278283
}
279284

src/util.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ static unsigned long id_or_max(const char *name)
133133
return id;
134134
}
135135

136+
// Checks if the name provided is a numeric value and does the conversion.
137+
bool hyper_name_to_id(const char *name, unsigned long *val)
138+
{
139+
char *ptr;
140+
errno = 0;
141+
long id = strtol(name, &ptr, 10);
142+
if (name == ptr || id < 0 || (errno != 0 && id == 0) || *ptr != '\0')
143+
return false;
144+
*val = id;
145+
return true;
146+
}
147+
136148
// the same as getpwnam(), but it only parses /etc/passwd and allows name to be id string
137149
struct passwd *hyper_getpwnam(const char *name)
138150
{

src/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdio.h>
55
#include <grp.h>
66
#include <pwd.h>
7+
#include <stdbool.h>
78
#include "../config.h"
89

910
struct hyper_pod;
@@ -36,6 +37,7 @@ int hyper_setfd_nonblock(int fd);
3637
int hyper_socketpair(int domain, int type, int protocol, int sv[2]);
3738
void hyper_shutdown();
3839
int hyper_insmod(char *module);
40+
bool hyper_name_to_id(const char *name, unsigned long *val);
3941
struct passwd *hyper_getpwnam(const char *name);
4042
struct group *hyper_getgrnam(const char *name);
4143
int hyper_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups);

0 commit comments

Comments
 (0)