Skip to content

Commit fac50e0

Browse files
authored
Merge pull request #562 from hpc/capfowner
dchmod: fix check for CAP_CHOWN and CAP_FOWNER capabilities
2 parents 7c40bff + b1676c1 commit fac50e0

File tree

2 files changed

+15
-38
lines changed

2 files changed

+15
-38
lines changed

src/common/mfu_flist.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,9 @@ void mfu_flist_metadata_apply(
550550
void mfu_flist_unlink(mfu_flist flist, bool traceless, mfu_file_t* mfu_file);
551551

552552
typedef struct {
553-
uid_t getuid; /* result from getuid */
554-
uid_t geteuid; /* result from geteuid */
555553
uid_t uid; /* new user id for item's owner, -1 for no change */
556554
gid_t gid; /* new group id for item's group, -1 for no change */
557555
mode_t umask; /* umask to apply when setting item permissions */
558-
bool capchown; /* whether process has CAP_CHOWN capability */
559-
bool capfowner; /* whether process has CAP_FOWNER capability */
560556
bool force; /* always call chmod/chgrp on every item */
561557
bool silence; /* avoid printing EPERM errors */
562558
} mfu_chmod_opts_t;

src/common/mfu_flist_chmod.c

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ static int chmod_list(
928928
const mfu_perms* head,
929929
const char* usrname,
930930
const char* grname,
931+
const mfu_proc_t* proc,
931932
mfu_chmod_opts_t* opts)
932933
{
933934
/* assume we'll succeed, set this to FAILURE on any error */
@@ -990,7 +991,7 @@ static int chmod_list(
990991
/* only attempt to change group if effective user id of
991992
* the process is the owner of the item or process has
992993
* CAP_CHWON capability */
993-
if (grname != NULL && opts->geteuid != olduid && !opts->capchown) {
994+
if (grname != NULL && proc->geteuid != olduid && !proc->cap_chown) {
994995
/* want to change group, but effective uid is not the
995996
* owner, linux prevents normal users from doing this */
996997
change = 0;
@@ -1069,7 +1070,7 @@ static int chmod_list(
10691070
/* don't bother changing permissions on files we don't own,
10701071
* unless process has CAP_FOWNER capability */
10711072
uid_t owner = (uid_t) mfu_flist_file_get_uid(list, idx);
1072-
if (opts->geteuid != owner && !opts->capfowner) {
1073+
if (proc->geteuid != owner && !proc->cap_fowner) {
10731074
/* don't attempt to change files we don't own */
10741075
change = 0;
10751076
}
@@ -1174,6 +1175,17 @@ void mfu_flist_chmod(
11741175
}
11751176
}
11761177

1178+
/* Determine whether process is running with CAP_CHOWN,
1179+
* allowing changes to uid/gid of file even when effective
1180+
* user id of the process does not match owner of the file. */
1181+
1182+
/* Determine whether process is running with CAP_FOWNER,
1183+
* allowing changes to permissions of file even when effective
1184+
* user id of the process does not match owner of the file */
1185+
1186+
mfu_proc_t proc;
1187+
mfu_proc_set(&proc);
1188+
11771189
/* wait for all tasks and start timer */
11781190
MPI_Barrier(MPI_COMM_WORLD);
11791191
double start_dchmod = MPI_Wtime();
@@ -1199,7 +1211,7 @@ void mfu_flist_chmod(
11991211

12001212
/* do a dchmod on each element in the list for this level & pass it the size */
12011213
uint64_t stats[7] = {0};
1202-
chmod_list(list, stats, head, usrname, grname, opts);
1214+
chmod_list(list, stats, head, usrname, grname, &proc, opts);
12031215

12041216
/* tally up stats for above operation into running totals */
12051217
total_stats[ITEM_COUNT] += stats[ITEM_COUNT];
@@ -1252,13 +1264,6 @@ mfu_chmod_opts_t* mfu_chmod_opts_new(void)
12521264
{
12531265
mfu_chmod_opts_t* opts = (mfu_chmod_opts_t*) MFU_MALLOC(sizeof(mfu_chmod_opts_t));
12541266

1255-
/* cache current real user id */
1256-
opts->getuid = getuid();
1257-
1258-
/* cache current effective user id,
1259-
* determines uid when considering owner ID of files */
1260-
opts->geteuid = geteuid();
1261-
12621267
/* chown with uid==-1 preserves the same owner,
12631268
* default to keeping the owner the same */
12641269
opts->uid = -1;
@@ -1271,30 +1276,6 @@ mfu_chmod_opts_t* mfu_chmod_opts_new(void)
12711276
* implied all in symbolic mode, like "+rX" */
12721277
opts->umask = 0;
12731278

1274-
/* whether process is running with CAP_CHOWN, allowing
1275-
* changes to uid/gid of file even when effective id
1276-
* of the process does not match the file */
1277-
opts->capchown = false;
1278-
#ifdef HAVE_LIBCAP
1279-
int cap_rc = cap_get_bound(CAP_CHOWN);
1280-
if (cap_rc > 0) {
1281-
/* process is running with CAP_CHOWN capability */
1282-
opts->capchown = true;
1283-
}
1284-
#endif
1285-
1286-
/* whether process is running with CAP_FOWNER, allowing
1287-
* changes to permissions of file even when effective user id
1288-
* of the process does not match the owner of the file */
1289-
opts->capfowner = false;
1290-
#ifdef HAVE_LIBCAP
1291-
cap_rc = cap_get_bound(CAP_FOWNER);
1292-
if (cap_rc > 0) {
1293-
/* process is running with CAP_FOWNER capability */
1294-
opts->capfowner = true;
1295-
}
1296-
#endif
1297-
12981279
/* avoid calling chmod/chown on all items,
12991280
* if this is set to true, call on every item */
13001281
opts->force = false;

0 commit comments

Comments
 (0)