X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Flib_internal%2Funify-unify.c;h=8c69cce2493597d2ff5c83022d09a9efe15a480f;hb=702ac5383430e4dd00f3610a78dadf6f84e4296b;hp=6423f5888ab0d4bb6773c506934e0701b6e83e2b;hpb=5269e9607d89ac4dbd34f9adb5b14fb5dfc9b1a5;p=util-vserver.git diff --git a/util-vserver/lib_internal/unify-unify.c b/util-vserver/lib_internal/unify-unify.c index 6423f58..8c69cce 100644 --- a/util-vserver/lib_internal/unify-unify.c +++ b/util-vserver/lib_internal/unify-unify.c @@ -30,43 +30,55 @@ #include bool -Unify_unify(char const *src, struct stat const *src_stat, - char const *dst, struct stat const UNUSED *dst_stat) +Unify_unify(char const *src, struct stat const UNUSED *src_stat, + char const *dst) { size_t l = strlen(dst); char tmpfile[l + sizeof(";XXXXXX")]; int fd; bool res = false; + struct stat st; // at first, set the ILI flags on 'src' - if (vc_set_iattr_compat(src, src_stat->st_dev, src_stat->st_ino, - 0, VC_IATTR_IUNLINK, VC_IATTR_IUNLINK, - &src_stat->st_mode)==-1) + if (vc_set_iattr(src, + 0, + VC_IATTR_IUNLINK|VC_IATTR_IMMUTABLE, + VC_IATTR_IUNLINK|VC_IATTR_IMMUTABLE)==-1) return false; - // now, create a temporary filename - memcpy(tmpfile, dst, l); - memcpy(tmpfile+l, ";XXXXXX", 8); - fd = mkstemp(tmpfile); - close(fd); + // check if 'dst' already exists + if (lstat(dst, &st)==0) { + // now, create a temporary filename + memcpy(tmpfile, dst, l); + memcpy(tmpfile+l, ";XXXXXX", 8); + fd = mkstemp(tmpfile); + close(fd); - if (fd==-1) { - perror("mkstemp()"); - return false; - } + if (fd==-1) { + perror("mkstemp()"); + return false; + } - // and rename the old file to this name - if (rename(dst, tmpfile)==-1) { - perror("rename()"); - goto err; + // and rename the old file to this name + + // NOTE: this rename() is race-free; when an attacker makes 'tmpfile' a + // directory, the operation would fail; when making it a symlink to a file + // or directory, the symlink but not the file/directory would be overridden + if (rename(dst, tmpfile)==-1) { + perror("rename()"); + goto err; + } } + else + tmpfile[0] = '\0'; // now, link the src-file to dst if (link(src, dst)==-1) { perror("link()"); unlink(dst); - if (rename(tmpfile, dst)==-1) { + if (tmpfile[0]!='\0' && + rename(tmpfile, dst)==-1) { perror("FATAL error in rename()"); _exit(1); } @@ -76,7 +88,8 @@ Unify_unify(char const *src, struct stat const *src_stat, res = true; err: - unlink(tmpfile); + if (tmpfile[0]!='\0') + unlink(tmpfile); return res; }