From 9ebaabeaac1a96b0d91f52902ce1dbf4f5a562dd Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Tue, 29 Sep 2020 14:55:02 -0300 Subject: sysvipc: Return EINVAL for invalid shmctl commands It avoids regressions on possible future commands that might require additional libc support. The downside is new commands added by newer kernels will need further glibc support. Checked on x86_64-linux-gnu and i686-linux-gnu (Linux v4.15 and v5.4). --- sysvipc/test-sysvipc.h | 33 ++++++++++++++++++++++++++++++--- sysvipc/test-sysvshm.c | 5 +++++ 2 files changed, 35 insertions(+), 3 deletions(-) (limited to 'sysvipc') diff --git a/sysvipc/test-sysvipc.h b/sysvipc/test-sysvipc.h index 21ef6c6..d1c8349 100644 --- a/sysvipc/test-sysvipc.h +++ b/sysvipc/test-sysvipc.h @@ -25,7 +25,7 @@ #include #include -/* Return the first invalid command SysV IPC command from common shared +/* Return the first invalid SysV IPC command from common shared between message queue, shared memory, and semaphore. */ static inline int first_common_invalid_cmd (void) @@ -50,7 +50,7 @@ first_common_invalid_cmd (void) return invalid; } -/* Return the first invalid command SysV IPC command for semaphore. */ +/* Return the first invalid SysV IPC command for semaphore. */ static inline int first_sem_invalid_cmd (void) { @@ -82,7 +82,7 @@ first_sem_invalid_cmd (void) return invalid; } -/* Return the first invalid command SysV IPC command for message queue. */ +/* Return the first invalid SysV IPC command for message queue. */ static inline int first_msg_invalid_cmd (void) { @@ -107,4 +107,31 @@ first_msg_invalid_cmd (void) return invalid; } +/* Return the first invalid SysV IPC command for shared memory. */ +static inline int +first_shm_invalid_cmd (void) +{ + const int shm_cmds[] = { + SHM_STAT, + SHM_INFO, +#ifdef SHM_STAT_ANY + SHM_STAT_ANY, +#endif + SHM_LOCK, + SHM_UNLOCK + }; + + int invalid = first_common_invalid_cmd (); + for (int i = 0; i < array_length (shm_cmds); i++) + { + if (invalid == shm_cmds[i]) + { + invalid++; + i = 0; + } + } + + return invalid; +} + #endif /* _TEST_SYSV_H */ diff --git a/sysvipc/test-sysvshm.c b/sysvipc/test-sysvshm.c index f083fd2..a1b8b48 100644 --- a/sysvipc/test-sysvshm.c +++ b/sysvipc/test-sysvshm.c @@ -25,6 +25,8 @@ #include #include +#include + #include #include #include @@ -81,6 +83,9 @@ do_test (void) FAIL_EXIT1 ("shmget failed (errno=%d)", errno); } + TEST_COMPARE (shmctl (shmid, first_shm_invalid_cmd (), NULL), -1); + TEST_COMPARE (errno, EINVAL); + /* Get shared memory kernel information and do some sanity checks. */ struct shmid_ds shminfo; if (shmctl (shmid, IPC_STAT, &shminfo) == -1) -- cgit v1.1