Skip to content

Commit

Permalink
chrootenv: resolve potential race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
lukateras committed Dec 28, 2017
1 parent 7798051 commit 25b35f4
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions pkgs/build-support/build-fhs-userenv/chrootenv.c
Expand Up @@ -124,16 +124,6 @@ int nftw_remove(const char *path, const struct stat *sb, int type,
return remove(path);
}

char *root;

void root_cleanup() {
if (nftw(root, nftw_remove, getdtablesize(),
FTW_DEPTH | FTW_MOUNT | FTW_PHYS) < 0)
errorf(EX_IOERR, "nftw(%s)", root);

free(root);
}

#define REQUIREMENTS \
"Requires Linux version >= 3.19 built with CONFIG_USER_NS option.\n"

Expand All @@ -158,6 +148,8 @@ int main(int argc, char *argv[]) {
if (temp == NULL)
temp = "/tmp";

char *root;

if (asprintf(&root, "%s/chrootenvXXXXXX", temp) < 0)
errorf(EX_IOERR, "asprintf");

Expand All @@ -166,8 +158,6 @@ int main(int argc, char *argv[]) {
if (root == NULL)
errorf(EX_IOERR, "mkdtemp(%s)", root);

atexit(root_cleanup);

// Don't make root private so that privilege drops inside chroot are possible:
if (chmod(root, 0755) < 0)
errorf(EX_IOERR, "chmod(%s, 0755)", root);
Expand Down Expand Up @@ -232,6 +222,12 @@ int main(int argc, char *argv[]) {
if (waitpid(cpid, &status, 0) < 0)
errorf(EX_OSERR, "waitpid(%d)", cpid);

if (nftw(root, nftw_remove, getdtablesize(),
FTW_DEPTH | FTW_MOUNT | FTW_PHYS) < 0)
errorf(EX_IOERR, "nftw(%s)", root);

free(root);

if (WIFEXITED(status)) {
return WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
Expand Down

0 comments on commit 25b35f4

Please sign in to comment.