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

Commit 7670d5e

Browse files
authored
Merge pull request #348 from keloyang/use-uid-number
Security: fix a issue (similar to runc CVE-2016-3697)
2 parents c820bc5 + ad6e433 commit 7670d5e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/util.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "container.h"
2727
#include "../config.h"
2828

29+
#define INVALID_UGID (~0UL)
30+
2931
int hyper_setup_env(struct env *envs, int num, bool setPATH)
3032
{
3133
int i, ret = 0;
@@ -152,17 +154,22 @@ bool hyper_name_to_id(const char *name, unsigned long *val)
152154
// the same as getpwnam(), but it only parses /etc/passwd and allows name to be id string
153155
struct passwd *hyper_getpwnam(const char *name)
154156
{
155-
uid_t uid = (uid_t)id_or_max(name);
156-
FILE *file = fopen("/etc/passwd", "r");
157+
uid_t uid;
158+
FILE *file;
159+
struct passwd *pwd;
160+
161+
uid = (uid_t)id_or_max(name);
162+
file = fopen("/etc/passwd", "r");
157163
if (!file) {
158164
perror("faile to open /etc/passwd");
159165
return NULL;
160166
}
161167
for (;;) {
162-
struct passwd *pwd = fgetpwent(file);
168+
pwd = fgetpwent(file);
163169
if (!pwd)
164170
break;
165-
if (!strcmp(pwd->pw_name, name) || pwd->pw_uid == uid) {
171+
if (pwd->pw_uid == uid ||
172+
(!strcmp(pwd->pw_name, name) && (uid_t)INVALID_UGID == uid)) {
166173
fclose(file);
167174
return pwd;
168175
}
@@ -174,17 +181,22 @@ struct passwd *hyper_getpwnam(const char *name)
174181
// the same as getgrnam(), but it only parses /etc/group and allows the name to be id string
175182
struct group *hyper_getgrnam(const char *name)
176183
{
177-
gid_t gid = (gid_t)id_or_max(name);
178-
FILE *file = fopen("/etc/group", "r");
184+
gid_t gid;
185+
FILE *file;
186+
struct group *gr = NULL;
187+
188+
gid = (gid_t)id_or_max(name);
189+
file = fopen("/etc/group", "r");
179190
if (!file) {
180191
perror("faile to open /etc/group");
181192
return NULL;
182193
}
183194
for (;;) {
184-
struct group *gr = fgetgrent(file);
195+
gr = fgetgrent(file);
185196
if (!gr)
186197
break;
187-
if (!strcmp(gr->gr_name, name) || gr->gr_gid == gid) {
198+
if (gr->gr_gid == gid ||
199+
(!strcmp(gr->gr_name, name) && (gid_t)INVALID_UGID == gid)) {
188200
fclose(file);
189201
return gr;
190202
}

0 commit comments

Comments
 (0)