Skip to contentSkip to conversation

lib/tst_cgroup: fix short reads of mems/cpus

PATCH New Jan Stancek 2 participants 5 messages Nov 3, 2020 – Nov 10, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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

---
 include/tst_cgroup.h                   | 6 ++++--
 lib/tst_cgroup.c                       | 6 ++++--
 testcases/kernel/mem/cpuset/cpuset01.c | 4 ++--
 3 files changed, 10 insertions(+), 6 deletions(-)
include/tst_cgroup.h +4 -2
index 77780e0d64c9..bfd8482608c4 100644
@@ -39,7 +39,9 @@ int  tst_cgroup_mem_swapacct_enabled(const char *cgroup_dir);
 void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz);
 
 /* Set of functions to read/write cpuset controller files content */
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename, char *retbuf);
-void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename, const char *buf);
+void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
+	char *retbuf, size_t retbuf_sz);
+void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename,
+	const char *buf);
 
 #endif /* TST_CGROUP_H */
lib/tst_cgroup.c +4 -2
index ba413d874f69..96c9524d2b3a 100644
@@ -393,7 +393,8 @@ void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz)
 		tst_cgroup_set_knob(cgroup_dir, "memory.swap.max", memsz);
 }
 
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename, char *retbuf)
+void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
+	char *retbuf, size_t retbuf_sz)
 {
 	int fd;
 	char *cgroup_new_dir;
@@ -417,7 +418,8 @@ void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
 			tst_brk(TBROK | TERRNO, "open %s", knob_path);
 	}
 
-	if (read(fd, retbuf, sizeof(retbuf)) < 0)
+	memset(retbuf, 0, retbuf_sz);
+	if (read(fd, retbuf, retbuf_sz) < 0)
 		tst_brk(TBROK | TERRNO, "read %s", knob_path);
 
 	close(fd);
testcases/kernel/mem/cpuset/cpuset01.c +2 -2
index f70ae0e486c2..fcd22040f7dd 100644
@@ -51,9 +51,9 @@ static void test_cpuset(void)
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	char mems[BUFSIZ], buf[BUFSIZ];
 
-	tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf);
+	tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf, sizeof(buf));
 	tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
-	tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems);
+	tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems, sizeof(buf));
 	tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "mems", mems);
 	tst_cgroup_move_current(PATH_TMP_CG_CST);
 
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/1 lib/tst_cgroup: fix short reads of mems/cpus

Quoted 93 lines · Jan Stancek

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

---
 include/tst_cgroup.h                   | 6 ++++--
 lib/tst_cgroup.c                       | 6 ++++--
 testcases/kernel/mem/cpuset/cpuset01.c | 4 ++--
 3 files changed, 10 insertions(+), 6 deletions(-)
include/tst_cgroup.h +0 -2
index 77780e0d64c9..bfd8482608c4 100644
@@ -39,7 +39,9 @@ int  tst_cgroup_mem_swapacct_enabled(const char
 *cgroup_dir);
 void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz);
 
 /* Set of functions to read/write cpuset controller files content */
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char
 *filename, char *retbuf);
-void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char

*filename, const char *buf);
+void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,

include/tst_cgroup.h +2 -0
+       char *retbuf, size_t retbuf_sz);
+void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char

*filename,

include/tst_cgroup.h +1 -0
+       const char *buf);
 
 #endif /* TST_CGROUP_H */
lib/tst_cgroup.c +2 -1
index ba413d874f69..96c9524d2b3a 100644
@@ -393,7 +393,8 @@ void tst_cgroup_mem_set_maxswap(const char
 *cgroup_dir, long memsz)
                tst_cgroup_set_knob(cgroup_dir, "memory.swap.max", memsz);
 }
 
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char
 *filename, char *retbuf)
+void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char
 *filename,
+       char *retbuf, size_t retbuf_sz)
 {
        int fd;
        char *cgroup_new_dir;
lib/tst_cgroup.c +2 -1
@@ -417,7 +418,8 @@ void tst_cgroup_cpuset_read_files(const char
 *cgroup_dir, const char *filename,
                        tst_brk(TBROK | TERRNO, "open %s", knob_path);
        }
 
-       if (read(fd, retbuf, sizeof(retbuf)) < 0)
+       memset(retbuf, 0, retbuf_sz);
+       if (read(fd, retbuf, retbuf_sz) < 0)
                tst_brk(TBROK | TERRNO, "read %s", knob_path);
 
        close(fd);
testcases/kernel/mem/cpuset/cpuset01.c +0 -0
index f70ae0e486c2..fcd22040f7dd 100644
@@ -51,9 +51,9 @@ static void test_cpuset(void)
        unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };

testcases/kernel/mem/cpuset/cpuset01.c static void test_cpuset(void) · line 59

Quoted 10 lines
testcases/kernel/mem/cpuset/cpuset01.c +2 -2
        char mems[BUFSIZ], buf[BUFSIZ];
 
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf,
 sizeof(buf));
        tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems,
 sizeof(buf));
 

sizeof() is generally used to calculate the size (in bytes) of a data type, e.g sizeof(char *).
I think here to pass 'BUFSIZ' directly is better than sizeof(buf).

Otherwise, looks good to me.

Quoted 7 lines
        tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "mems", mems);
        tst_cgroup_move_current(PATH_TMP_CG_CST);

--
2.18.1

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201104/ea1346c6/attachment.htm>
#2 lore raw

PATCH 1/1 lib/tst_cgroup: fix short reads of mems/cpus

Quoted 10 lines · Li Wang

...

Quoted 8 lines
(quoted diff) +1 -1
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf,
sizeof(buf));
        tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
(quoted diff) +1 -1
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems,

sizeof(buf));

Quoted 4 lines

sizeof() is generally used to calculate the size (in bytes) of a data type, e.g sizeof(char *).
I think here to pass 'BUFSIZ' directly is better than sizeof(buf).

Jan, do you agree with this point?
If yes, I can help to modify and apply this patch.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201109/b2c2e584/attachment.htm>
#3 lore raw

PATCH 1/1 lib/tst_cgroup: fix short reads of mems/cpus

----- Original Message -----

Quoted 12 lines · Li Wang
Quoted 11 lines

...

Quoted 8 lines
(quoted diff) +1 -1
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf,
sizeof(buf));
        tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
(quoted diff) +1 -1
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems,

sizeof(buf));

Quoted 7 lines
Quoted 4 lines

sizeof() is generally used to calculate the size (in bytes) of a data type, e.g sizeof(char *).
I think here to pass 'BUFSIZ' directly is better than sizeof(buf).

Jan, do you agree with this point?
If yes, I can help to modify and apply this patch.

2nd line should have been sizeof(mems), but BUFSIZ will work too.

#4 lore raw

PATCH 1/1 lib/tst_cgroup: fix short reads of mems/cpus

Quoted 17 lines · Jan Stancek

----- Original Message -----

Quoted 12 lines · Li Wang
Quoted 11 lines

...

Quoted 8 lines
(quoted diff) +1 -1
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf,
sizeof(buf));
        tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
(quoted diff) +1 -1
-       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems);
+       tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems,

sizeof(buf));

Quoted 1 line
Quoted 1 line

sizeof() is generally used to calculate the size (in bytes) of a data

type,

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

e.g sizeof(char *).
I think here to pass 'BUFSIZ' directly is better than sizeof(buf).

Jan, do you agree with this point?
If yes, I can help to modify and apply this patch.

2nd line should have been sizeof(mems), but BUFSIZ will work too.

Pushed, thanks!

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201110/0ea7c10f/attachment-0001.htm>