Skip to content

Commit 1f011e5

Browse files
Richard Palethorpemetan-ucw
authored andcommitted
read_all: Drop privileges
The LTP is usually run as root, which allows read_all_dev to read files which are usually protected from being read at random. This patch introduces the -p switch to read_all which is used to drop privileges (switch to the nobody user) for the read_all_dev test. If -p is set, but the current user does not have the capabilities to change the uid and gid, then the test will continue under the current user. This allows the most common scenarios to work as expected, but may cause difficulties for someone running the LTP under a semi-privileged user. Signed-off-by: Richard Palethorpe <[email protected]> Signed-off-by: Cyril Hrubis <[email protected]>
1 parent 5c5c1b6 commit 1f011e5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

runtest/fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fs_di fs_di -d $TMPDIR
6969
# Was not sure why it should reside in runtest/crashme and won´t get tested ever
7070
proc01 proc01 -m 128
7171

72-
read_all_dev read_all -d /dev -e '/dev/watchdog?(0)' -q -r 10
72+
read_all_dev read_all -d /dev -p -q -r 10
7373
read_all_proc read_all -d /proc -q -r 10
7474
read_all_sys read_all -d /sys -q -r 10
7575

testcases/kernel/fs/read_all/read_all.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <fnmatch.h>
5151
#include <semaphore.h>
5252
#include <ctype.h>
53+
#include <pwd.h>
5354

5455
#include "tst_test.h"
5556

@@ -88,6 +89,7 @@ static long worker_count;
8889
static char *str_max_workers;
8990
static long max_workers = 15;
9091
static struct worker *workers;
92+
static char *drop_privs;
9193

9294
static struct tst_option options[] = {
9395
{"v", &verbose,
@@ -104,6 +106,8 @@ static struct tst_option options[] = {
104106
"-w count Set the worker count limit, the default is 15."},
105107
{"W:", &str_worker_count,
106108
"-W count Override the worker count. Ignores (-w) and the processor count."},
109+
{"p", &drop_privs,
110+
"-p Drop privileges; switch to the nobody user."},
107111
{NULL, NULL, NULL}
108112
};
109113

@@ -247,6 +251,24 @@ static int worker_run(struct worker *self)
247251
return 0;
248252
}
249253

254+
static void maybe_drop_privs(void)
255+
{
256+
struct passwd *nobody;
257+
258+
if (!drop_privs)
259+
return;
260+
261+
nobody = SAFE_GETPWNAM("nobody");
262+
263+
TEST(setgid(nobody->pw_gid));
264+
if (TEST_RETURN < 0 && TEST_ERRNO != EPERM)
265+
tst_brk(TBROK | TTERRNO, "Failed to use nobody gid");
266+
267+
TEST(setuid(nobody->pw_uid));
268+
if (TEST_RETURN < 0 && TEST_ERRNO != EPERM)
269+
tst_brk(TBROK | TTERRNO, "Failed to use nobody uid");
270+
}
271+
250272
static void spawn_workers(void)
251273
{
252274
int i;
@@ -257,8 +279,10 @@ static void spawn_workers(void)
257279
for (i = 0; i < worker_count; i++) {
258280
wa[i].q = queue_init();
259281
wa[i].pid = SAFE_FORK();
260-
if (!wa[i].pid)
282+
if (!wa[i].pid) {
283+
maybe_drop_privs();
261284
exit(worker_run(wa + i));
285+
}
262286
}
263287
}
264288

0 commit comments

Comments
 (0)