Skip to content

Commit 6cb356c

Browse files
committed
basic/fs-util: add a version of chmod_and_chown that doesn not use /proc
1 parent 08c7c32 commit 6cb356c

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/basic/fs-util.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,52 @@ int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) {
272272
return do_chown || do_chmod;
273273
}
274274

275+
int chmod_and_chown_unsafe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
276+
bool do_chown, do_chmod;
277+
struct stat st;
278+
279+
assert(path);
280+
281+
/* Change ownership and access mode of the specified path, see description of fchmod_and_chown().
282+
* Should only be used on trusted paths. */
283+
284+
if (lstat(path, &st) < 0)
285+
return -errno;
286+
287+
do_chown =
288+
(uid != UID_INVALID && st.st_uid != uid) ||
289+
(gid != GID_INVALID && st.st_gid != gid);
290+
291+
do_chmod =
292+
!S_ISLNK(st.st_mode) && /* chmod is not defined on symlinks */
293+
((mode != MODE_INVALID && ((st.st_mode ^ mode) & 07777) != 0) ||
294+
do_chown); /* If we change ownership, make sure we reset the mode afterwards, since chown()
295+
* modifies the access mode too */
296+
297+
if (mode == MODE_INVALID)
298+
mode = st.st_mode; /* If we only shall do a chown(), save original mode, since chown() might break it. */
299+
else if ((mode & S_IFMT) != 0 && ((mode ^ st.st_mode) & S_IFMT) != 0)
300+
return -EINVAL; /* insist on the right file type if it was specified */
301+
302+
if (do_chown && do_chmod) {
303+
mode_t minimal = st.st_mode & mode; /* the subset of the old and the new mask */
304+
305+
if (((minimal ^ st.st_mode) & 07777) != 0)
306+
if (chmod(path, minimal & 07777) < 0)
307+
return -errno;
308+
}
309+
310+
if (do_chown)
311+
if (lchown(path, uid, gid) < 0)
312+
return -errno;
313+
314+
if (do_chmod)
315+
if (chmod(path, mode & 07777) < 0)
316+
return -errno;
317+
318+
return do_chown || do_chmod;
319+
}
320+
275321
int fchmod_umask(int fd, mode_t m) {
276322
mode_t u;
277323
int r;

src/basic/fs-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ int readlink_and_make_absolute(const char *p, char **r);
3434

3535
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
3636
int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid);
37+
int chmod_and_chown_unsafe(const char *path, mode_t mode, uid_t uid, gid_t gid);
3738

3839
int fchmod_umask(int fd, mode_t mode);
3940
int fchmod_opath(int fd, mode_t m);

src/test/test-fs-util.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,50 @@ static void test_chmod_and_chown(void) {
802802
assert_se(S_ISLNK(st.st_mode));
803803
}
804804

805+
static void test_chmod_and_chown_unsafe(void) {
806+
_cleanup_(rm_rf_physical_and_freep) char *d = NULL;
807+
_unused_ _cleanup_umask_ mode_t u = umask(0000);
808+
struct stat st;
809+
const char *p;
810+
811+
if (geteuid() != 0)
812+
return;
813+
814+
log_info("/* %s */", __func__);
815+
816+
assert_se(mkdtemp_malloc(NULL, &d) >= 0);
817+
818+
p = strjoina(d, "/reg");
819+
assert_se(mknod(p, S_IFREG | 0123, 0) >= 0);
820+
821+
assert_se(chmod_and_chown_unsafe(p, S_IFREG | 0321, 1, 2) >= 0);
822+
assert_se(chmod_and_chown_unsafe(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
823+
824+
assert_se(lstat(p, &st) >= 0);
825+
assert_se(S_ISREG(st.st_mode));
826+
assert_se((st.st_mode & 07777) == 0321);
827+
828+
p = strjoina(d, "/dir");
829+
assert_se(mkdir(p, 0123) >= 0);
830+
831+
assert_se(chmod_and_chown_unsafe(p, S_IFDIR | 0321, 1, 2) >= 0);
832+
assert_se(chmod_and_chown_unsafe(p, S_IFREG | 0555, 3, 4) == -EINVAL);
833+
834+
assert_se(lstat(p, &st) >= 0);
835+
assert_se(S_ISDIR(st.st_mode));
836+
assert_se((st.st_mode & 07777) == 0321);
837+
838+
p = strjoina(d, "/lnk");
839+
assert_se(symlink("idontexist", p) >= 0);
840+
841+
assert_se(chmod_and_chown_unsafe(p, S_IFLNK | 0321, 1, 2) >= 0);
842+
assert_se(chmod_and_chown_unsafe(p, S_IFREG | 0555, 3, 4) == -EINVAL);
843+
assert_se(chmod_and_chown_unsafe(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
844+
845+
assert_se(lstat(p, &st) >= 0);
846+
assert_se(S_ISLNK(st.st_mode));
847+
}
848+
805849
int main(int argc, char *argv[]) {
806850
test_setup_logging(LOG_INFO);
807851

@@ -819,6 +863,7 @@ int main(int argc, char *argv[]) {
819863
test_fsync_directory_of_file();
820864
test_rename_noreplace();
821865
test_chmod_and_chown();
866+
test_chmod_and_chown_unsafe();
822867

823868
return 0;
824869
}

0 commit comments

Comments
 (0)