Skip to content

Commit

Permalink
Only call chown if our effective userid is root
Browse files Browse the repository at this point in the history
  • Loading branch information
jprjr committed Jan 15, 2021
1 parent 5ef2a7a commit 999e193
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/s6-overlay-preinit/s6-overlay-preinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,27 @@ int main (void)
}
}

/* requirement: /var/run/s6 must be owned by current user */
/* requirement: /var/run/s6 must be owned by current user,
* if (and only if) current user != root */
uid_t uid = getuid() ,
gid = getgid() ,
euid = geteuid() ;

struct stat s6stat ;
if(stat(VAR_RUN_S6, &s6stat) == -1)
{
strerr_diefu2sys(111, "stat ", VAR_RUN_S6) ;
}

uid_t uid = getuid() ,
gid = getgid() ;
/* only call chown if uid/gid are not from current user */
if(s6stat.st_uid != uid || s6stat.st_gid != gid)
{
if (chown(VAR_RUN_S6, uid, gid) == -1)
if (euid == 0) /* chown will fail if we're not root */
{
strerr_diefu2sys(111, "chown ", VAR_RUN_S6) ;
if (chown(VAR_RUN_S6, uid, gid) == -1)
{
strerr_diefu2sys(111, "chown ", VAR_RUN_S6) ;
}
}
}

Expand Down

0 comments on commit 999e193

Please sign in to comment.