aboutsummaryrefslogtreecommitdiffstats
path: root/main/fortify-headers/0001-fix-realpath-when-stdlib.h-is-included-before-limits.patch
blob: 17d0624ee2f1b86eb26792de70505e7acec93058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
From 31d62b60c5b4f1baa795537da898a83e39be9dd1 Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Thu, 7 May 2015 14:41:36 +0200
Subject: [PATCH fortify-headers] fix realpath when stdlib.h is included before
 limits.h
To: sin@2f30.org

If program includes stdlib.h before limits.h without _XOPEN_SOURCE,
_GNU_SOURCE or _BSD_SOURCE explicitly set, then will it always trigger
the trap with musl libc.

This is becase stdlib.h will pull in features.h which will set
_GNU_SOURCE. This means that the fortify stdlib.h will not include
limits.h but it will still trigger the fortified realpath(), but without
PATH_MAX set.

We fix this by including system stdlib.h before testing if limits.h
should be included.

Since PATH_MAX is known at compile time we can also error at compile
time, instead of compiling a broken realpath().
---
 include/stdlib.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h
index 22f1f2b..6629652 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -1,12 +1,12 @@
 #ifndef _FORTIFY_STDLIB_H
 #define _FORTIFY_STDLIB_H
 
+#include_next <stdlib.h>
+
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #include_next <limits.h>
 #endif
 
-#include_next <stdlib.h>
-
 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
 
 #ifdef __cplusplus
@@ -23,7 +23,7 @@ char *realpath(const char *path, char *resolved)
 
 	if (resolved) {
 #ifndef PATH_MAX
-		__builtin_trap();
+# error PATH_MAX unset. A fortified realpath will not work.
 #else
 		bos = __builtin_object_size(resolved, 0);
 		if (PATH_MAX > bos)
-- 
2.4.0