SysV IPC bug reproducer
v5 cover SysV IPC bug reproducer
From: Andrea Cervesato <[email protected]>
This patch series provides a bug reproducer for SysV IPC, which has been written by Michal Hocko.
It also includes the SAFE_MPROTECT macro, useful for many other tests as well.
Andrea Cervesato (3):
Add SAFE_MPROTECT() macro
Print prot flag when SAFE_MMAP() fails
Add shmat04 SysV IPC bug reproducer
include/tst_safe_macros.h | 70 +++++++++++++- runtest/syscalls | 1 + runtest/syscalls-ipc | 1 + .../kernel/syscalls/ipc/shmat/.gitignore | 1 + testcases/kernel/syscalls/ipc/shmat/shmat04.c | 92 +++++++++++++++++++ 5 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 testcases/kernel/syscalls/ipc/shmat/shmat04.c
Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v5 1/3 Add SAFE_MPROTECT() macro
From: Andrea Cervesato <[email protected]>
---
Moved SAFE_MPROTECT() macro into tst_safe_macros.h
include/tst_safe_macros.h | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
include/tst_safe_macros.h +60 -0
@@ -6,6 +6,7 @@#ifndef TST_SAFE_MACROS_H__ #define TST_SAFE_MACROS_H__ +#include <stdlib.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/time.h>
@@ -268,6 +269,36 @@ int safe_getgroups(const char *file, const int lineno, int size, gid_t list[]);* -D_FILE_OFFSET_BITS=64 compile flag */ +#define PROT_FLAG_STR (f) #f " | " + +static void prot_to_str (const int prot,char *buf) +{ +char *ptr = buf; + +if (prot ==PROT_NONE ) { +strcpy (buf,"PROT_NONE" ); +return ; + } + +if (prot &PROT_READ ) { +strcpy (ptr,PROT_FLAG_STR (PROT_READ )); + ptr +=sizeof (PROT_FLAG_STR (PROT_READ )) -1 ; + } + +if (prot &PROT_WRITE ) { +strcpy (ptr,PROT_FLAG_STR (PROT_WRITE )); + ptr +=sizeof (PROT_FLAG_STR (PROT_WRITE )) -1 ; + } + +if (prot &PROT_EXEC ) { +strcpy (ptr,PROT_FLAG_STR (PROT_EXEC )); + ptr +=sizeof (PROT_FLAG_STR (PROT_EXEC )) -1 ; + } + +if (buf != ptr) + ptr[-3 ] =0 ; +} +static inline void *safe_mmap (const char *file,const int lineno,void *addr,size_t length,int prot,int flags,int fd,off_t offset)
@@ -287,6 +318,35 @@ static inline void *safe_mmap(const char *file, const int lineno,safe_mmap (__FILE__, __LINE__, (addr), (length), (prot), \ (flags), (fd), (offset)) +static inline int safe_mprotect (const char *file,const int lineno, +char *addr,size_t len,int prot) +{ +int rval; +char *prot_buf; + + prot_buf = (char *)safe_malloc (file, lineno,0 ,512 ); +prot_to_str (prot, prot_buf); + +tst_res_ (file, lineno,TDEBUG , +"mprotect(%p, %ld, %s(%x))" , addr, len, prot_buf, prot); + +free (prot_buf); + + rval =mprotect (addr, len, prot); + +if (rval ==-1 ) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"mprotect() failed" ); + }else if (rval) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"Invalid mprotect() return value %d" , rval); + } + +return rval; +} +#define SAFE_MPROTECT (addr, len, prot) \ + safe_mprotect(__FILE__, __LINE__, (addr), (len), (prot)) +static inline int safe_ftruncate (const char *file,const int lineno,int fd,off_t length) {
Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v5 1/3] Add SAFE_MPROTECT() macro
Hi!
include/tst_safe_macros.h int safe_getgroups(const char *file, const int lineno, int size, gid_t list[]); · line 276
Quoted 4 lines
include/tst_safe_macros.h +3
-0+static void prot_to_str (const int prot,char *buf) +{ +char *ptr = buf;
I would still put an explicit check for the buffer size here, so that we are sure that the complete combination of READ|WRITE|EXEC would fit, but I guess that it's fine as long as this is an internal static function and not part of the API.
Quoted 28 lines
include/tst_safe_macros.h +24
-0+if (prot ==PROT_NONE ) { +strcpy (buf,"PROT_NONE" ); +return ; + } + +if (prot &PROT_READ ) { +strcpy (ptr,PROT_FLAG_STR (PROT_READ )); + ptr +=sizeof (PROT_FLAG_STR (PROT_READ )) -1 ; + } + +if (prot &PROT_WRITE ) { +strcpy (ptr,PROT_FLAG_STR (PROT_WRITE )); + ptr +=sizeof (PROT_FLAG_STR (PROT_WRITE )) -1 ; + } + +if (prot &PROT_EXEC ) { +strcpy (ptr,PROT_FLAG_STR (PROT_EXEC )); + ptr +=sizeof (PROT_FLAG_STR (PROT_EXEC )) -1 ; + } + +if (buf != ptr) + ptr[-3 ] =0 ; +} +static inline void *safe_mmap (const char *file,const int lineno,void *addr,size_t length,int prot,int flags,int fd,off_t offset)
include/tst_safe_macros.h static inline void *safe_mmap(const char *file, const int lineno, · line 327
Quoted 10 lines
include/tst_safe_macros.h +7
-0@@ -287,6 +318,35 @@ static inline void *safe_mmap(const char *file, const int lineno,safe_mmap (__FILE__, __LINE__, (addr), (length), (prot), \ (flags), (fd), (offset)) +static inline int safe_mprotect (const char *file,const int lineno, +char *addr,size_t len,int prot) +{ +int rval; +char *prot_buf; + + prot_buf = (char *)safe_malloc (file, lineno,0 ,512 );
Why are we allocating the buffer? Why not just prot_buf[512] ?
Also the cast to (char*) is never needed in C as void* is automatically converted to any type of a pointer without explicit cast.
Otherwise it looks good. You can add my Reviewed-by: if you change the malloc to an array on the stack.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v5 1/3] Add SAFE_MPROTECT() macro
In reply to Andrea CervesatoHi!
Quoted 13 lines
include/tst_safe_macros.h +13
-0+static inline int safe_mprotect (const char *file,const int lineno, +char *addr,size_t len,int prot) +{ +int rval; +char *prot_buf; + + prot_buf = (char *)safe_malloc (file, lineno,0 ,512 ); +prot_to_str (prot, prot_buf); + +tst_res_ (file, lineno,TDEBUG , +"mprotect(%p, %ld, %s(%x))" , addr, len, prot_buf, prot); + +free (prot_buf);
include/tst_safe_macros.h static inline void *safe_mmap(const char *file, const int lineno, · line 343
Quoted 10 lines
include/tst_safe_macros.h +10
-0+ + rval =mprotect (addr, len, prot); + +if (rval ==-1 ) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"mprotect() failed" ); + }else if (rval) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"Invalid mprotect() return value %d" , rval); + }
Ah, and can we please print the whole parameter list for mprotect() in these two cases as well?
Quoted 14 lines
include/tst_safe_macros.h +5
-0+return rval; +} +#define SAFE_MPROTECT (addr, len, prot) \ + safe_mprotect(__FILE__, __LINE__, (addr), (len), (prot)) +static inline int safe_ftruncate (const char *file,const int lineno,int fd,off_t length) {Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v5 2/3 Print prot flag when SAFE_MMAP() fails
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
--- include/tst_safe_macros.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
include/tst_safe_macros.h +8 -2
@@ -304,12 +304,18 @@ static inline void *safe_mmap(const char *file, const int lineno, int prot, int flags, int fd, off_t offset) { void *rval; +char *prot_buf; rval =mmap (addr, length, prot, flags, fd, offset);if (rval ==MAP_FAILED ) { + prot_buf = (char *)safe_malloc (file, lineno,0 ,512 ); +prot_to_str (prot, prot_buf); +tst_brk_ (file, lineno,TBROK |TERRNO , -"mmap(%p,%zu,%d,%d,%d,%ld) failed" , - addr, length, prot, flags, fd, (long ) offset); +"mmap(%p,%zu,%s(%x),%d,%d,%ld) failed" , + addr, length, prot_buf, prot, flags, fd, (long ) offset); + +free (prot_buf); }return rval;
Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v5 2/3] Print prot flag when SAFE_MMAP() fails
Hi!
include/tst_safe_macros.h static inline void *safe_mmap(const char *file, const int lineno, · line 307
Quoted 12 lines
include/tst_safe_macros.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)include/tst_safe_macros.h +1
-0@@ -304,12 +304,18 @@ static inline void *safe_mmap(const char *file, const int lineno, int prot, int flags, int fd, off_t offset) { void *rval; +char *prot_buf;
Shouldn't we add the TDEBUG message here?
include/tst_safe_macros.h static inline void *safe_mmap(const char *file, const int lineno, · line 318
Quoted 12 lines
include/tst_safe_macros.h +7
-2rval =mmap (addr, length, prot, flags, fd, offset);if (rval ==MAP_FAILED ) { + prot_buf = (char *)safe_malloc (file, lineno,0 ,512 ); +prot_to_str (prot, prot_buf); +tst_brk_ (file, lineno,TBROK |TERRNO , -"mmap(%p,%zu,%d,%d,%d,%ld) failed" , - addr, length, prot, flags, fd, (long ) offset); +"mmap(%p,%zu,%s(%x),%d,%d,%ld) failed" , + addr, length, prot_buf, prot, flags, fd, (long ) offset); + +free (prot_buf);
This is fine as long as we switch to an on the stack array.
Quoted 9 lines
include/tst_safe_macros.h +0
-0}return rval;Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v5 3/3 Add shmat04 SysV IPC bug reproducer
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
This is an LTP port of a SysV bug reproducer provided by Michal Hocko.
When debugging issues with a workload using SysV shmem, Michal Hocko has come up with a reproducer that shows how a series of mprotect() operations can result in an elevated shm_nattch and thus leak of the resource.
---
Ensure that GETIPCKEY() will give a free slot given IPC key + 1
runtest/syscalls | 1 + runtest/syscalls-ipc | 1 + .../kernel/syscalls/ipc/shmat/.gitignore | 1 + testcases/kernel/syscalls/ipc/shmat/shmat04.c | 92 +++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 testcases/kernel/syscalls/ipc/shmat/shmat04.c
runtest/syscalls +1 -0
@@ -1444,6 +1444,7 @@ setxattr03 setxattr03 shmat01 shmat01 shmat02 shmat02 shmat03 shmat03 +shmat04 shmat04 shmctl01 shmctl01 shmctl02 shmctl02
runtest/syscalls-ipc +1 -0
@@ -49,6 +49,7 @@ semop03 semop03 shmat01 shmat01 shmat02 shmat02 +shmat04 shmat04 shmctl01 shmctl01 shmctl02 shmctl02
testcases/kernel/syscalls/ipc/shmat/.gitignore +1 -0
@@ -1,3 +1,4 @@ /shmat01 /shmat02 /shmat03 +/shmat04
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +92 -0
92 lines not on this page · raw message
Signature · 5 lines
2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp