Skip to contentSkip to conversation

safe_macros: Fix undefined behaviour in vararg handling

PATCH Superseded Tudor Cretu 3 participants 9 messages Nov 29, 2022 – Nov 30, 2022 Library code

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover safe_macros: Fix undefined behaviour in vararg handling

Accessing elements in an empty va_list results in undefined behaviour[0] that can include accessing arbitrary stack memory. While typically this doesn't raise a fault, some new more security-oriented architectures (e.g. CHERI[1] or Morello[2]) don't allow it.

Therefore, remove the variadicness from safe_* wrappers that always call the functions with the optional argument included.

Adapt the respective SAFE_* macros to handle the change by passing a default argument if they're omitted.

[0]: [ISO/IEC 9899:2011] Programming Languages—C, 3rd ed, paragraph 7.16.1.1 [1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
[2]: https://www.morello-project.org/

v2..v1:
- PATCH 1: Remove the NULL argument for mode from SAFE_OPEN instances to avoid the pointer to int conversion.

Tudor Cretu (3):
safe_open: Fix undefined behaviour in vararg handling
safe_openat: Fix undefined behaviour in vararg handling
safe_semctl: Fix undefined behaviour in vararg handling

 include/old/safe_macros.h                         |  6 ++++--
 include/safe_macros_fn.h                          |  3 ++-
 include/tst_safe_file_at.h                        | 10 ++++++----
 include/tst_safe_macros.h                         |  6 ++++--
 include/tst_safe_sysv_ipc.h                       | 14 +++++++++-----
 lib/safe_macros.c                                 | 13 +------------
 lib/tst_cgroup.c                                  |  2 +-
 lib/tst_safe_file_at.c                            | 11 +++--------
 lib/tst_safe_sysv_ipc.c                           | 10 +---------
 testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
 14 files changed, 36 insertions(+), 49 deletions(-)
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v2 1/3 safe_open: Fix undefined behaviour in vararg handling

Accessing elements in an empty va_list is undefined behaviour. Therefore, remove the variadicness from safe_open as it always calls open with the mode argument included.

Adapt the SAFE_OPEN macro to handle the change by passing a default argument of 0 to mode if it's omitted.

Some instances of SAFE_OPEN were passing NULL as the mode argument instead of an int. Remove it and allow the macros to handle it to avoid a pointer to int conversion.

---
 include/old/safe_macros.h                         |  6 ++++--
 include/safe_macros_fn.h                          |  3 ++-
 include/tst_safe_macros.h                         |  6 ++++--
 lib/safe_macros.c                                 | 13 +------------
 testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
 9 files changed, 16 insertions(+), 22 deletions(-)
include/old/safe_macros.h +4 -2
index fb1d7a110..d16540d63 100644
@@ -59,9 +59,11 @@
 #define SAFE_MUNMAP(cleanup_fn, addr, length)	\
 	safe_munmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length))
 
+#define __SAFE_OPEN(cleanup_fn, pathname, oflags, mode, ...)	\
+	safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), (mode))
+
 #define SAFE_OPEN(cleanup_fn, pathname, oflags, ...)	\
-	safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \
-	    ##__VA_ARGS__)
+	__SAFE_OPEN((cleanup_fn), (pathname), (oflags), ##__VA_ARGS__, 0)
 
 #define SAFE_PIPE(cleanup_fn, fildes)	\
 	safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes))
include/safe_macros_fn.h +2 -1
index 114d8fd43..d143079c3 100644
@@ -74,7 +74,8 @@ int safe_munmap(const char *file, const int lineno,
                 void (*cleanup_fn)(void), void *addr, size_t length);
 
 int safe_open(const char *file, const int lineno,
-              void (*cleanup_fn)(void), const char *pathname, int oflags, ...);
+              void (*cleanup_fn)(void), const char *pathname, int oflags,
+              mode_t mode);
 
 int safe_pipe(const char *file, const int lineno,
               void (*cleanup_fn)(void), int fildes[2]);
include/tst_safe_macros.h +4 -2
index 81c4b0844..d53555c88 100644
@@ -86,9 +86,11 @@ void *safe_realloc(const char *file, const int lineno, void *ptr, size_t size);
 #define SAFE_MUNMAP(addr, length) \
 	safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
 
+#define __SAFE_OPEN(pathname, oflags, mode, ...) \
+	safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), (mode))
+
 #define SAFE_OPEN(pathname, oflags, ...) \
-	safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \
-	    ##__VA_ARGS__)
+	__SAFE_OPEN((pathname), (oflags), ##__VA_ARGS__, 0)
 
 #define SAFE_PIPE(fildes) \
 	safe_pipe(__FILE__, __LINE__, NULL, (fildes))
lib/safe_macros.c +1 -12
index d8816631f..a92b58347 100644
@@ -234,20 +234,9 @@ int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
 }
 
 int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
-              const char *pathname, int oflags, ...)
+              const char *pathname, int oflags, mode_t mode)
 {
-	va_list ap;
 	int rval;
-	mode_t mode;
-
-	va_start(ap, oflags);
-
-	/* Android's NDK's mode_t is smaller than an int, which results in
-	 * SIGILL here when passing the mode_t type.
-	 */
-	mode = va_arg(ap, int);
-
-	va_end(ap);
 
 	rval = open(pathname, oflags, mode);
 
testcases/kernel/syscalls/fgetxattr/fgetxattr01.c +1 -1
index 35c46a1c3..52e6e44ab 100644
@@ -111,7 +111,7 @@ static void setup(void)
 	size_t i = 0;
 
 	SAFE_TOUCH(FNAME, 0644, NULL);
-	fd = SAFE_OPEN(FNAME, O_RDONLY, NULL);
+	fd = SAFE_OPEN(FNAME, O_RDONLY);
 
 	for (i = 0; i < ARRAY_SIZE(tc); i++) {
 		tc[i].value = SAFE_MALLOC(tc[i].size);
testcases/kernel/syscalls/fgetxattr/fgetxattr02.c +1 -1
index 82fb676be..c3cff0aab 100644
@@ -244,7 +244,7 @@ static void setup(void)
 			SAFE_BIND(tc[i].fd, (const struct sockaddr *) &sun,
 					sizeof(struct sockaddr_un));
 		} else {
-			tc[i].fd = SAFE_OPEN(tc[i].fname, tc[i].fflags, NULL);
+			tc[i].fd = SAFE_OPEN(tc[i].fname, tc[i].fflags);
 		}
 
 		if (tc[i].exp_ret >= 0) {
testcases/kernel/syscalls/fgetxattr/fgetxattr03.c +1 -1
index d293ffca9..0581b9670 100644
@@ -48,7 +48,7 @@ static void verify_fgetxattr(void)
 static void setup(void)
 {
 	SAFE_TOUCH(FILENAME, 0644, NULL);
-	fd = SAFE_OPEN(FILENAME, O_RDONLY, NULL);
+	fd = SAFE_OPEN(FILENAME, O_RDONLY);
 
 	SAFE_FSETXATTR(fd, XATTR_TEST_KEY, XATTR_TEST_VALUE,
 			XATTR_TEST_VALUE_SIZE, XATTR_CREATE);
testcases/kernel/syscalls/fsetxattr/fsetxattr01.c +1 -1
index ffec8104f..d799e477f 100644
@@ -205,7 +205,7 @@ static void setup(void)
 	long_value[XATTR_SIZE_MAX + 1] = '\0';
 
 	SAFE_TOUCH(FNAME, 0644, NULL);
-	fd = SAFE_OPEN(FNAME, O_RDONLY, NULL);
+	fd = SAFE_OPEN(FNAME, O_RDONLY);
 
 	for (i = 0; i < ARRAY_SIZE(tc); i++) {
 		if (!tc[i].key)
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +1 -1
index 3aea4b59e..0336c964a 100644
@@ -211,7 +211,7 @@ static void setup(void)
 	for (i = 0; i < ARRAY_SIZE(tc); i++) {
 
 		if (!tc[i].issocket) {
-			tc[i].fd = SAFE_OPEN(tc[i].fname, tc[i].fflags, NULL);
+			tc[i].fd = SAFE_OPEN(tc[i].fname, tc[i].fflags);
 			continue;
 		}
 
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v2 2/3 safe_openat: Fix undefined behaviour in vararg handling

In reply to Tudor Cretu

Accessing elements in an empty va_list is undefined behaviour. Therefore, remove the variadicness from safe_openat as it always calls openat with the mode argument included.

Adapt the SAFE_OPENAT macro to handle the change by passing a default argument of 0 to mode if it's omitted.

---
 include/tst_safe_file_at.h | 10 ++++++----
 lib/tst_cgroup.c           |  2 +-
 lib/tst_safe_file_at.c     | 11 +++--------
 3 files changed, 10 insertions(+), 13 deletions(-)
include/tst_safe_file_at.h +6 -4
index a1aa19fad..dd43d8f65 100644
@@ -11,9 +11,11 @@
 #include <unistd.h>
 #include <stdarg.h>
 
-#define SAFE_OPENAT(dirfd, path, oflags, ...)			\
-	safe_openat(__FILE__, __LINE__,				\
-		    (dirfd), (path), (oflags), ## __VA_ARGS__)
+#define __SAFE_OPENAT(dirfd, path, oflags, mode, ...)			\
+	safe_openat(__FILE__, __LINE__, (dirfd), (path), (oflags), (mode))
+
+#define SAFE_OPENAT(dirfd, path, oflags, ...)				\
+	__SAFE_OPENAT((dirfd), (path), (oflags), ##__VA_ARGS__, 0)
 
 #define SAFE_FILE_READAT(dirfd, path, buf, nbyte)			\
 	safe_file_readat(__FILE__, __LINE__,				\
@@ -38,7 +40,7 @@ const char *tst_decode_fd(const int fd)
 			  __attribute__((warn_unused_result));
 
 int safe_openat(const char *const file, const int lineno, const int dirfd,
-                const char *const path, const int oflags, ...)
+                const char *const path, const int oflags, const mode_t mode)
 		__attribute__((nonnull, warn_unused_result));
 
 ssize_t safe_file_readat(const char *const file, const int lineno,
lib/tst_cgroup.c +1 -1
index 50699bc63..9831bc336 100644
@@ -1345,7 +1345,7 @@ int safe_cg_open(const char *const file, const int lineno,
 		if (!alias)
 			continue;
 
-		fds[i++] = safe_openat(file, lineno, (*dir)->dir_fd, alias, flags);
+		fds[i++] = safe_openat(file, lineno, (*dir)->dir_fd, alias, flags, 0);
 	}
 
 	return i;
lib/tst_safe_file_at.c +3 -8
index f530dc349..9b8944f01 100644
@@ -33,15 +33,10 @@ const char *tst_decode_fd(const int fd)
 }
 
 int safe_openat(const char *const file, const int lineno,
-		const int dirfd, const char *const path, const int oflags, ...)
+		const int dirfd, const char *const path, const int oflags,
+		const mode_t mode)
 {
-	va_list ap;
 	int fd;
-	mode_t mode;
-
-	va_start(ap, oflags);
-	mode = va_arg(ap, int);
-	va_end(ap);
 
 	fd = openat(dirfd, path, oflags, mode);
 	if (fd > -1)
@@ -58,7 +53,7 @@ ssize_t safe_file_readat(const char *const file, const int lineno,
 			 const int dirfd, const char *const path,
 			 char *const buf, const size_t nbyte)
 {
-	int fd = safe_openat(file, lineno, dirfd, path, O_RDONLY);
+	int fd = safe_openat(file, lineno, dirfd, path, O_RDONLY, 0);
 	ssize_t rval;
 
 	if (fd < 0)
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 3/3 safe_semctl: Fix undefined behaviour in vararg handling

In reply to Tudor Cretu

Accessing elements in an empty va_list is undefined behaviour. Therefore, remove the variadicness from safe_semctl as it always calls semctl with the union semun argument included.

Adapt the SAFE_SEMCTL macro to handle the change by passing a zero-initialised union semun if it's omitted.

---
 include/tst_safe_sysv_ipc.h | 14 +++++++++-----
 lib/tst_safe_sysv_ipc.c     | 10 +---------
 2 files changed, 10 insertions(+), 14 deletions(-)
include/tst_safe_sysv_ipc.h +9 -5
index 7804ce192..976a30409 100644
@@ -10,6 +10,7 @@
 #include <sys/msg.h>
 #include <sys/shm.h>
 #include <sys/sem.h>
+#include "lapi/sem.h"
 
 int safe_msgget(const char *file, const int lineno, key_t key, int msgflg);
 #define SAFE_MSGGET(key, msgflg) \
@@ -58,11 +59,14 @@ int safe_semget(const char *file, const int lineno, key_t key, int nsems,
 	safe_semget(__FILE__, __LINE__, (key), (nsems), (semflg))
 
 int safe_semctl(const char *file, const int lineno, int semid, int semnum,
-		int cmd, ...);
-#define SAFE_SEMCTL(semid, semnum, cmd, ...) ({ \
-	int tst_ret_ = safe_semctl(__FILE__, __LINE__, (semid), (semnum), \
-				(cmd), ##__VA_ARGS__); \
-	(semid) = ((cmd) == IPC_RMID ? -1 : (semid)); \
+		int cmd, union semun un);
+#define __SAFE_SEMCTL(semid, semnum, cmd, un, ...) \
+	safe_semctl(__FILE__, __LINE__, (semid), (semnum), (cmd), (un))
+
+#define SAFE_SEMCTL(semid, semnum, cmd, ...) ({					\
+	int tst_ret_ = __SAFE_SEMCTL((semid), (semnum), (cmd), ##__VA_ARGS__,	\
+				     (union semun){0});				\
+	(semid) = ((cmd) == IPC_RMID ? -1 : (semid));				\
 	tst_ret_; })
 
 int safe_semop(const char *file, const int lineno, int semid, struct sembuf *sops,
lib/tst_safe_sysv_ipc.c +1 -9
index 5eaa82539..f99f6db5e 100644
@@ -228,17 +228,9 @@ int safe_semget(const char *file, const int lineno, key_t key, int nsems,
 }
 
 int safe_semctl(const char *file, const int lineno, int semid, int semnum,
-		int cmd, ...)
+		int cmd, union semun un)
 {
 	int rval;
-	va_list va;
-	union semun un;
-
-	va_start(va, cmd);
-
-	un = va_arg(va, union semun);
-
-	va_end(va);
 
 	rval = semctl(semid, semnum, cmd, un);
 
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on the v2 series
#4 lore raw
In reply to Tudor Cretu

Hello,

So I'm happy with this, but I think Cyril's comment deserves a response:

Quoted 10 lines

Looking at how glibc handles this, the code looks like:

	int mode = 0;

	if (__OPEN_NEEDS_MODE(oflag)) {
		..
		mode = va_arg(arg, int);
		..
	}
Quoted 2 lines

That sounds much easier than messing with the macros and should avoid undefined behavior.

I don't see why, __OPEN_NEEDS_MODE is going to be different between functions and libc/kernel versions.

Quoted 42 lines · Tudor Cretu

Accessing elements in an empty va_list results in undefined behaviour[0] that can include accessing arbitrary stack memory. While typically this doesn't raise a fault, some new more security-oriented architectures (e.g. CHERI[1] or Morello[2]) don't allow it.

Therefore, remove the variadicness from safe_* wrappers that always call the functions with the optional argument included.

Adapt the respective SAFE_* macros to handle the change by passing a default argument if they're omitted.

[0]: [ISO/IEC 9899:2011] Programming Languages—C, 3rd ed, paragraph 7.16.1.1 [1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
[2]: https://www.morello-project.org/

v2..v1:
- PATCH 1: Remove the NULL argument for mode from SAFE_OPEN instances to avoid the pointer to int conversion.

Tudor Cretu (3):
safe_open: Fix undefined behaviour in vararg handling
safe_openat: Fix undefined behaviour in vararg handling
safe_semctl: Fix undefined behaviour in vararg handling

 include/old/safe_macros.h                         |  6 ++++--
 include/safe_macros_fn.h                          |  3 ++-
 include/tst_safe_file_at.h                        | 10 ++++++----
 include/tst_safe_macros.h                         |  6 ++++--
 include/tst_safe_sysv_ipc.h                       | 14 +++++++++-----
 lib/safe_macros.c                                 | 13 +------------
 lib/tst_cgroup.c                                  |  2 +-
 lib/tst_safe_file_at.c                            | 11 +++--------
 lib/tst_safe_sysv_ipc.c                           | 10 +---------
 testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
 14 files changed, 36 insertions(+), 49 deletions(-)
Signature · 1 line
2.25.1
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hi all,

Quoted 3 lines

Hello,

So I'm happy with this, but I think Cyril's comment deserves a response:

+1

Quoted 4 lines
Quoted 3 lines

Looking at how glibc handles this, the code looks like:

	int mode = 0;
Quoted 8 lines
Quoted 8 lines
	if (__OPEN_NEEDS_MODE(oflag)) {
		..
		mode = va_arg(arg, int);
		..
	}

That sounds much easier than messing with the macros and should avoid undefined behavior.

+1

Quoted 3 lines

I don't see why, __OPEN_NEEDS_MODE is going to be different between

functions and libc/kernel versions.

Looking at glibc's __OPEN_NEEDS_MODE definition, the logic is obviously the same as musl code for open (it just use O_TMPFILE instead of __O_TMPFILE therefore no need to check for #ifdef __O_TMPFILE).

Kind regards,
Petr

Quoted 46 lines
Quoted 42 lines · Tudor Cretu

Accessing elements in an empty va_list results in undefined behaviour[0] that can include accessing arbitrary stack memory. While typically this doesn't raise a fault, some new more security-oriented architectures (e.g. CHERI[1] or Morello[2]) don't allow it.

Therefore, remove the variadicness from safe_* wrappers that always call the functions with the optional argument included.

Adapt the respective SAFE_* macros to handle the change by passing a default argument if they're omitted.

[0]: [ISO/IEC 9899:2011] Programming Languages—C, 3rd ed, paragraph 7.16.1.1 [1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
[2]: https://www.morello-project.org/

v2..v1:
- PATCH 1: Remove the NULL argument for mode from SAFE_OPEN instances to avoid the pointer to int conversion.

Tudor Cretu (3):
safe_open: Fix undefined behaviour in vararg handling
safe_openat: Fix undefined behaviour in vararg handling
safe_semctl: Fix undefined behaviour in vararg handling

 include/old/safe_macros.h                         |  6 ++++--
 include/safe_macros_fn.h                          |  3 ++-
 include/tst_safe_file_at.h                        | 10 ++++++----
 include/tst_safe_macros.h                         |  6 ++++--
 include/tst_safe_sysv_ipc.h                       | 14 +++++++++-----
 lib/safe_macros.c                                 | 13 +------------
 lib/tst_cgroup.c                                  |  2 +-
 lib/tst_safe_file_at.c                            | 11 +++--------
 lib/tst_safe_sysv_ipc.c                           | 10 +---------
 testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
 testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
 testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
 14 files changed, 36 insertions(+), 49 deletions(-)
Signature · 1 line
2.25.1
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
Quoted 5 lines · Petr Vorel

Hi all,

Quoted 3 lines

Hello,

So I'm happy with this, but I think Cyril's comment deserves a response:

Indeed, I noticed it too late after sending the v2.

Quoted 14 lines

+1

Quoted 12 lines
Quoted 12 lines

Looking at how glibc handles this, the code looks like:

	int mode = 0;

	if (__OPEN_NEEDS_MODE(oflag)) {
		..
		mode = va_arg(arg, int);
		..
	}

That sounds much easier than messing with the macros and should avoid undefined behavior.

I considered this and I think it's better to focus strictly on the handling the variadicness issue, and wanted to avoid duplicating logic from libcs.

Quoted 5 lines

+1

Quoted 3 lines

I don't see why, __OPEN_NEEDS_MODE is going to be different between

functions and libc/kernel versions.

Haven't thought about that, that's a good point in my opinion.

Quoted 4 lines

Looking at glibc's __OPEN_NEEDS_MODE definition, the logic is obviously the same as musl code for open (it just use O_TMPFILE instead of __O_TMPFILE therefore no need to check for #ifdef __O_TMPFILE).

I agree, for open/openat this approach would be fairly simple, there is semctl too though, I'll need to have a look how glibc and musl handle it.

Kind regards,
Tudor

Quoted 50 lines

Kind regards,
Petr

Quoted 46 lines
Quoted 42 lines · Tudor Cretu

Accessing elements in an empty va_list results in undefined behaviour[0] that can include accessing arbitrary stack memory. While typically this doesn't raise a fault, some new more security-oriented architectures (e.g. CHERI[1] or Morello[2]) don't allow it.

Therefore, remove the variadicness from safe_* wrappers that always call the functions with the optional argument included.

Adapt the respective SAFE_* macros to handle the change by passing a default argument if they're omitted.

[0]: [ISO/IEC 9899:2011] Programming Languages—C, 3rd ed, paragraph 7.16.1.1 [1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
[2]: https://www.morello-project.org/

v2..v1:
- PATCH 1: Remove the NULL argument for mode from SAFE_OPEN instances to avoid the pointer to int conversion.

Tudor Cretu (3):
safe_open: Fix undefined behaviour in vararg handling
safe_openat: Fix undefined behaviour in vararg handling
safe_semctl: Fix undefined behaviour in vararg handling

  include/old/safe_macros.h                         |  6 ++++--
  include/safe_macros_fn.h                          |  3 ++-
  include/tst_safe_file_at.h                        | 10 ++++++----
  include/tst_safe_macros.h                         |  6 ++++--
  include/tst_safe_sysv_ipc.h                       | 14 +++++++++-----
  lib/safe_macros.c                                 | 13 +------------
  lib/tst_cgroup.c                                  |  2 +-
  lib/tst_safe_file_at.c                            | 11 +++--------
  lib/tst_safe_sysv_ipc.c                           | 10 +---------
  testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
  testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
  testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
  testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
  testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
  14 files changed, 36 insertions(+), 49 deletions(-)
Signature · 1 line
2.25.1
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
Quoted 38 lines · Petr Vorel
Quoted 5 lines

Hi all,

Quoted 3 lines

Hello,

So I'm happy with this, but I think Cyril's comment deserves a response:

Indeed, I noticed it too late after sending the v2.

Quoted 14 lines

+1

Quoted 12 lines
Quoted 12 lines

Looking at how glibc handles this, the code looks like:

	int mode = 0;

	if (__OPEN_NEEDS_MODE(oflag)) {
		..
		mode = va_arg(arg, int);
		..
	}

That sounds much easier than messing with the macros and should avoid undefined behavior.

I considered this and I think it's better to focus strictly on the handling the variadicness issue, and wanted to avoid duplicating logic from libcs.

Quoted 5 lines

+1

Quoted 3 lines

I don't see why, __OPEN_NEEDS_MODE is going to be different between

functions and libc/kernel versions.

Haven't thought about that, that's a good point in my opinion.

Quoted 6 lines
Quoted 3 lines

Looking at glibc's __OPEN_NEEDS_MODE definition, the logic is obviously the same as musl code for open (it just use O_TMPFILE instead of __O_TMPFILE therefore no need to check for #ifdef __O_TMPFILE).

I agree, for open/openat this approach would be fairly simple, there is semctl too though, I'll need to have a look how glibc and musl handle it.

Thanks a lot for your time Tudor!

Kind regards,
Petr

Quoted 53 lines

Kind regards,
Tudor

Quoted 49 lines

Kind regards,
Petr

Quoted 46 lines
Quoted 42 lines · Tudor Cretu

Accessing elements in an empty va_list results in undefined behaviour[0] that can include accessing arbitrary stack memory. While typically this doesn't raise a fault, some new more security-oriented architectures (e.g. CHERI[1] or Morello[2]) don't allow it.

Therefore, remove the variadicness from safe_* wrappers that always call the functions with the optional argument included.

Adapt the respective SAFE_* macros to handle the change by passing a default argument if they're omitted.

[0]: [ISO/IEC 9899:2011] Programming Languages—C, 3rd ed, paragraph 7.16.1.1 [1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
[2]: https://www.morello-project.org/

v2..v1:
- PATCH 1: Remove the NULL argument for mode from SAFE_OPEN instances to avoid the pointer to int conversion.

Tudor Cretu (3):
safe_open: Fix undefined behaviour in vararg handling
safe_openat: Fix undefined behaviour in vararg handling
safe_semctl: Fix undefined behaviour in vararg handling

  include/old/safe_macros.h                         |  6 ++++--
  include/safe_macros_fn.h                          |  3 ++-
  include/tst_safe_file_at.h                        | 10 ++++++----
  include/tst_safe_macros.h                         |  6 ++++--
  include/tst_safe_sysv_ipc.h                       | 14 +++++++++-----
  lib/safe_macros.c                                 | 13 +------------
  lib/tst_cgroup.c                                  |  2 +-
  lib/tst_safe_file_at.c                            | 11 +++--------
  lib/tst_safe_sysv_ipc.c                           | 10 +---------
  testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
  testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
  testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
  testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
  testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
  14 files changed, 36 insertions(+), 49 deletions(-)
Signature · 1 line
2.25.1
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw
Quoted 38 lines · Petr Vorel
Quoted 36 lines · Petr Vorel
Quoted 5 lines

Hi all,

Quoted 3 lines

Hello,

So I'm happy with this, but I think Cyril's comment deserves a response:

Indeed, I noticed it too late after sending the v2.

Quoted 14 lines

+1

Quoted 12 lines
Quoted 12 lines

Looking at how glibc handles this, the code looks like:

	int mode = 0;

	if (__OPEN_NEEDS_MODE(oflag)) {
		..
		mode = va_arg(arg, int);
		..
	}

That sounds much easier than messing with the macros and should avoid undefined behavior.

I considered this and I think it's better to focus strictly on the handling the variadicness issue, and wanted to avoid duplicating logic from libcs.

Quoted 5 lines

+1

Quoted 3 lines

I don't see why, __OPEN_NEEDS_MODE is going to be different between

functions and libc/kernel versions.

Haven't thought about that, that's a good point in my opinion.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Looking at glibc's __OPEN_NEEDS_MODE definition, the logic is obviously the same as musl code for open (it just use O_TMPFILE instead of __O_TMPFILE therefore no need to check for #ifdef __O_TMPFILE).

I agree, for open/openat this approach would be fairly simple, there is semctl too though, I'll need to have a look how glibc and musl handle it.

Thanks a lot for your time Tudor!

My pleasure! Many thanks for the feedback, all. I have posted a v3. I look forward to your opinions.

Kind regards,
Tudor

Quoted 57 lines

Kind regards,
Petr

Quoted 53 lines

Kind regards,
Tudor

Quoted 49 lines

Kind regards,
Petr

Quoted 46 lines
Quoted 42 lines · Tudor Cretu

Accessing elements in an empty va_list results in undefined behaviour[0] that can include accessing arbitrary stack memory. While typically this doesn't raise a fault, some new more security-oriented architectures (e.g. CHERI[1] or Morello[2]) don't allow it.

Therefore, remove the variadicness from safe_* wrappers that always call the functions with the optional argument included.

Adapt the respective SAFE_* macros to handle the change by passing a default argument if they're omitted.

[0]: [ISO/IEC 9899:2011] Programming Languages—C, 3rd ed, paragraph 7.16.1.1 [1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
[2]: https://www.morello-project.org/

v2..v1:
- PATCH 1: Remove the NULL argument for mode from SAFE_OPEN instances to avoid the pointer to int conversion.

Tudor Cretu (3):
safe_open: Fix undefined behaviour in vararg handling
safe_openat: Fix undefined behaviour in vararg handling
safe_semctl: Fix undefined behaviour in vararg handling

   include/old/safe_macros.h                         |  6 ++++--
   include/safe_macros_fn.h                          |  3 ++-
   include/tst_safe_file_at.h                        | 10 ++++++----
   include/tst_safe_macros.h                         |  6 ++++--
   include/tst_safe_sysv_ipc.h                       | 14 +++++++++-----
   lib/safe_macros.c                                 | 13 +------------
   lib/tst_cgroup.c                                  |  2 +-
   lib/tst_safe_file_at.c                            | 11 +++--------
   lib/tst_safe_sysv_ipc.c                           | 10 +---------
   testcases/kernel/syscalls/fgetxattr/fgetxattr01.c |  2 +-
   testcases/kernel/syscalls/fgetxattr/fgetxattr02.c |  2 +-
   testcases/kernel/syscalls/fgetxattr/fgetxattr03.c |  2 +-
   testcases/kernel/syscalls/fsetxattr/fsetxattr01.c |  2 +-
   testcases/kernel/syscalls/fsetxattr/fsetxattr02.c |  2 +-
   14 files changed, 36 insertions(+), 49 deletions(-)
Signature · 1 line
2.25.1
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp