]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredump filter: fix stack overflow with =all
authorLuca Boccassi <bluca@debian.org>
Wed, 26 Apr 2023 13:18:04 +0000 (14:18 +0100)
committerLuca Boccassi <bluca@debian.org>
Wed, 26 Apr 2023 14:04:06 +0000 (15:04 +0100)
We translate 'all' to UNIT64_MAX, which has a lot more 'f's. Use the
helper macro, since a decimal uint64_t will always be >> than a hex
representation.

root@image:~# systemd-run -t --property CoredumpFilter=all ls /tmp
Running as unit: run-u13.service
Press ^] three times within 1s to disconnect TTY.
*** stack smashing detected ***: terminated
[137256.320511] systemd[1]: run-u13.service: Main process exited, code=dumped, status=6/ABRT
[137256.320850] systemd[1]: run-u13.service: Failed with result 'core-dump'.

src/basic/macro.h
src/shared/coredump-util.c

index 99262e920654dcc60d6f4e81fa9c0148874e1065..13620a60bcaaae125011ccc75b320288f3b7540b 100644 (file)
@@ -250,6 +250,10 @@ static inline int __coverity_check_and_return__(int condition) {
 #define sizeof_field(struct_type, member) sizeof(((struct_type *) 0)->member)
 #define endoffsetof_field(struct_type, member) (offsetof(struct_type, member) + sizeof_field(struct_type, member))
 
+/* Maximum buffer size needed for formatting an unsigned integer type as hex, including space for '0x'
+ * prefix and trailing NUL suffix. */
+#define HEXADECIMAL_STR_MAX(type) (2 + sizeof(type) * 2 + 1)
+
 /* Returns the number of chars needed to format variables of the specified type as a decimal string. Adds in
  * extra space for a negative '-' prefix for signed types. Includes space for the trailing NUL. */
 #define DECIMAL_STR_MAX(type)                                           \
index bf8ea00b14cf00f72153d9859bc36f6465181cd6..dc137317a90449edb30ee43d15bccf97f1a05be5 100644 (file)
@@ -158,9 +158,9 @@ int parse_auxv(int log_level,
 }
 
 int set_coredump_filter(uint64_t value) {
-        char t[STRLEN("0xFFFFFFFF")];
+        char t[HEXADECIMAL_STR_MAX(uint64_t)];
 
-        sprintf(t, "0x%"PRIx64, value);
+        xsprintf(t, "0x%"PRIx64, value);
 
         return write_string_file("/proc/self/coredump_filter", t,
                                  WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);