@@ -192,37 +192,25 @@ struct hyper_event_ops err_ops = {
192192
193193static int hyper_setup_exec_user (struct hyper_exec * exec )
194194{
195- char * user = exec -> user == NULL || strlen (exec -> user ) == 0 ? NULL : exec -> user ;
195+ char * user = exec -> user == NULL || strlen (exec -> user ) == 0 ? "0" : exec -> user ;
196196 char * group = exec -> group == NULL || strlen (exec -> group ) == 0 ? NULL : exec -> group ;
197197
198198 uid_t uid = 0 ;
199199 gid_t gid = 0 ;
200200 int ngroups = 0 ;
201201 gid_t * reallocgroups , * groups = NULL ;
202202
203- // check the config
204- if (!user && !group && exec -> nr_additional_groups == 0 ) {
205- return 0 ;
206- }
207-
208203 // get uid
209- if (user ) {
210- fprintf (stdout , "try to find the user: %s\n" , user );
211- struct passwd * pwd = hyper_getpwnam (user );
212- if (pwd == NULL ) {
213- unsigned long id ;
214- if (!hyper_name_to_id (user , & id )) {
215- perror ("can't find the user" );
216- return -1 ;
217- }
218- uid = id ;
219- goto get_gid ;
220- }
204+ fprintf (stdout , "try to find the user(or uid): %s\n" , user );
205+ struct passwd * pwd = hyper_getpwnam (user );
206+ if (pwd != NULL ) {
221207 uid = pwd -> pw_uid ;
222208 gid = pwd -> pw_gid ;
209+ fprintf (stdout , "found the user: %s, uid:%d, gid:%d\n" , user , uid , gid );
223210
224211 // get groups of user
225- groups = malloc (sizeof (gid_t ) * 10 );
212+ ngroups = 10 ;
213+ groups = malloc (sizeof (gid_t ) * ngroups );
226214 if (groups == NULL ) {
227215 goto fail ;
228216 }
@@ -236,26 +224,20 @@ static int hyper_setup_exec_user(struct hyper_exec *exec)
236224 goto fail ;
237225 }
238226 }
227+ fprintf (stdout , "get %d groups from /etc/group\n" , ngroups );
239228
240229 // set user related envs. the container env config can overwrite it
241230 setenv ("USER" , pwd -> pw_name , 1 );
242231 setenv ("HOME" , pwd -> pw_dir , 1 );
243232 } else {
244- ngroups = getgroups (0 , NULL );
245- if (ngroups < 0 ) {
246- goto fail ;
247- }
248- groups = malloc (sizeof (gid_t ) * ngroups );
249- if (groups == NULL ) {
250- goto fail ;
251- }
252- ngroups = getgroups (ngroups , groups );
253- if (ngroups < 0 ) {
254- goto fail ;
233+ unsigned long id ;
234+ if (!hyper_name_to_id (user , & id )) {
235+ perror ("can't find the user" );
236+ return -1 ;
255237 }
238+ uid = id ;
256239 }
257240
258- get_gid :
259241 // get gid
260242 if (group ) {
261243 fprintf (stdout , "try to find the group: %s\n" , group );
@@ -296,24 +278,30 @@ static int hyper_setup_exec_user(struct hyper_exec *exec)
296278
297279 // setup the owner of tty
298280 if (exec -> tty ) {
281+ gid_t tty_gid = gid ;
299282 char ptmx [512 ];
300283 sprintf (ptmx , "/dev/pts/%d" , exec -> ptyno );
301- if (chown (ptmx , uid , gid ) < 0 ) {
284+
285+ struct group * gr = hyper_getgrnam ("tty" );
286+ if (gr != NULL ) {
287+ tty_gid = gr -> gr_gid ;
288+ }
289+ if (chown (ptmx , uid , tty_gid ) < 0 ) {
302290 perror ("failed to change the owner for the slave pty file" );
303291 goto fail ;
304292 }
305293 }
306294
307295 // apply
308- if (groups && setgroups (ngroups , groups ) < 0 ) {
296+ if (ngroups > 0 && setgroups (ngroups , groups ) < 0 ) {
309297 perror ("setgroups() fails" );
310298 goto fail ;
311299 }
312- if (setgid (gid ) < 0 ) {
300+ if (gid > 0 && setgid (gid ) < 0 ) {
313301 perror ("setgid() fails" );
314302 goto fail ;
315303 }
316- if (setuid (uid ) < 0 ) {
304+ if (uid > 0 && setuid (uid ) < 0 ) {
317305 perror ("setuid() fails" );
318306 goto fail ;
319307 }
0 commit comments