Skip to content

Commit 80c26b8

Browse files
jstancekwangli5665
authored andcommitted
lib/tst_cgroup: fix short reads of mems/cpus
tst_cgroup_cpuset_read_files() is wrongly using sizeof on pointer type to figure out length of buffer. On some systems there's more content in cgroup mems/cpus, which leads to partial read and subsequent EINVAL on write. Also the retval buffer should be zero terminated, since buffer can be passed uninitialized, subsequently leading to writing garbage to cgroup and again hitting EINVAL. Before: tst_test.c:1250: TINFO: Timeout per run is 0h 05m 00s tst_device.c:423: TINFO: No device is mounted at cgroup2 tst_device.c:423: TINFO: No device is mounted at /tmp/cgroup_cst tst_cgroup.c:100: TINFO: Cgroup(cpuset) v1 mount at /tmp/cgroup_cst success safe_macros.c:458: TBROK: tst_cgroup.c:449: write(6,0x7fffc0425410,18446744073709551615) failed: EINVAL (22) tst_cgroup.c:173: TWARN: umount /tmp/cgroup_cst: EBUSY (16) tst_cgroup.c:175: TWARN: rmdir /tmp/cgroup_cst: EBUSY (16) tst_cgroup.c:178: TINFO: Cgroup v1 unmount success After: tst_test.c:1250: TINFO: Timeout per run is 0h 05m 00s tst_device.c:423: TINFO: No device is mounted at cgroup2 tst_device.c:423: TINFO: No device is mounted at /tmp/cgroup_cst tst_cgroup.c:100: TINFO: Cgroup(cpuset) v1 mount at /tmp/cgroup_cst success cpuset01.c:83: TPASS: cpuset test pass tst_cgroup.c:178: TINFO: Cgroup v1 unmount success Signed-off-by: Jan Stancek <[email protected]> Reviewed-by: Li Wang <[email protected]>
1 parent 16595bf commit 80c26b8

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

include/tst_cgroup.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ int tst_cgroup_mem_swapacct_enabled(const char *cgroup_dir);
3939
void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz);
4040

4141
/* Set of functions to read/write cpuset controller files content */
42-
void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename, char *retbuf);
43-
void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename, const char *buf);
42+
void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
43+
char *retbuf, size_t retbuf_sz);
44+
void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename,
45+
const char *buf);
4446

4547
#endif /* TST_CGROUP_H */

lib/tst_cgroup.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz)
393393
tst_cgroup_set_knob(cgroup_dir, "memory.swap.max", memsz);
394394
}
395395

396-
void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename, char *retbuf)
396+
void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
397+
char *retbuf, size_t retbuf_sz)
397398
{
398399
int fd;
399400
char *cgroup_new_dir;
@@ -417,7 +418,8 @@ void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
417418
tst_brk(TBROK | TERRNO, "open %s", knob_path);
418419
}
419420

420-
if (read(fd, retbuf, sizeof(retbuf)) < 0)
421+
memset(retbuf, 0, retbuf_sz);
422+
if (read(fd, retbuf, retbuf_sz) < 0)
421423
tst_brk(TBROK | TERRNO, "read %s", knob_path);
422424

423425
close(fd);

testcases/kernel/mem/cpuset/cpuset01.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ static void test_cpuset(void)
5151
unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
5252
char mems[BUFSIZ], buf[BUFSIZ];
5353

54-
tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf);
54+
tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf, BUFSIZ);
5555
tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
56-
tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems);
56+
tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems, BUFSIZ);
5757
tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "mems", mems);
5858
tst_cgroup_move_current(PATH_TMP_CG_CST);
5959

0 commit comments

Comments
 (0)