summaryrefslogtreecommitdiffstats
path: root/util/kconfig/patches/0012-safer-tmpfiles.patch
blob: d9b836181240760e480d6d938918af2d650071fb (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
commit 7b2deddbb0ef350e189fe42c025b07c943aedc4c
Author: Raul E Rangel <rrangel@chromium.org>
Date:   Thu Jul 25 15:49:52 2019 -0600

    Kconfig: Write tmp files into same directory as target files

    This removes the need for COREBOOT_BUILD_DIR in Kconfig. Since the
    original files will be replaced with the tmp file, the parent directory
    already needs to be writable.

    Before this change, the tmp files would be created in the CWD (src) if
    COREBOOT_BUILD_DIR was not specified.

    BUG=b:112267918
    TEST=emerge-grunt coreboot and verified no tmp files were created in the
    src directory.

    Change-Id: Icdaf2ff3dd1ec98813b75ef55b96e38e1ca19ec7
    Signed-off-by: Raul E Rangel <rrangel@chromium.org>
    Reviewed-on: https://review.coreboot.org/c/coreboot/+/34244
    Reviewed-by: Martin Roth <martinroth@google.com>
    Tested-by: build bot (Jenkins) <no-reply@coreboot.org>

Index: kconfig/confdata.c
===================================================================
--- kconfig.orig/confdata.c
+++ kconfig/confdata.c
@@ -880,6 +880,16 @@ next_menu:
 	return 0;
 }
 
+
+static int conf_mktemp(const char *path, char *tmpfile)
+{
+	if (snprintf(tmpfile, PATH_MAX, "%s.tmp.XXXXXX", path) >= PATH_MAX) {
+		errno = EOVERFLOW;
+		return -1;
+	}
+	return mkstemp(tmpfile);
+}
+
 int conf_write(const char *name)
 {
 	FILE *out;
@@ -1001,7 +1011,14 @@ static int conf_write_dep(const char *na
 	struct file *file;
 	FILE *out;
 
-	out = fopen("..config.tmp", "w");
+	if (make_parent_dir(name))
+		return 1;
+	char filename[PATH_MAX];
+	int fd = conf_mktemp(name, filename);
+	if (fd == -1)
+		return 1;
+
+	out = fdopen(fd, "w");
 	if (!out)
 		return 1;
 	fprintf(out, "deps_config := \\\n");
@@ -1019,9 +1036,7 @@ static int conf_write_dep(const char *na
 	fprintf(out, "\n$(deps_config): ;\n");
 	fclose(out);
 
-	if (make_parent_dir(name))
-		return 1;
-	rename("..config.tmp", name);
+	rename(filename, name);
 	return 0;
 }
 
@@ -1117,11 +1132,26 @@ int conf_write_autoconf(int overwrite)
 	if (conf_touch_deps())
 		return 1;
 
-	out = fopen(".tmpconfig", "w");
+	if (make_parent_dir(autoconf_name))
+		return 1;
+	char filename[PATH_MAX];
+	int fd = conf_mktemp(autoconf_name, filename);
+	if (fd == -1)
+		return 1;
+	out = fdopen(fd, "w");
 	if (!out)
 		return 1;
 
-	out_h = fopen(".tmpconfig.h", "w");
+	name = getenv("KCONFIG_AUTOHEADER");
+	if (!name)
+		name = "include/generated/autoconf.h";
+	if (make_parent_dir(name))
+		return 1;
+	char filename_h[PATH_MAX];
+	int fd_h = conf_mktemp(name, filename_h);
+	if (fd_h == -1)
+		return 1;
+	out_h = fdopen(fd_h, "w");
 	if (!out_h) {
 		fclose(out);
 		return 1;
@@ -1144,21 +1174,14 @@ int conf_write_autoconf(int overwrite)
 	fclose(out);
 	fclose(out_h);
 
-	name = getenv("KCONFIG_AUTOHEADER");
-	if (!name)
-		name = "include/generated/autoconf.h";
-	if (make_parent_dir(name))
-		return 1;
-	if (rename(".tmpconfig.h", name))
+	if (rename(filename_h, name))
 		return 1;
 
-	if (make_parent_dir(autoconf_name))
-		return 1;
 	/*
 	 * This must be the last step, kbuild has a dependency on auto.conf
 	 * and this marks the successful completion of the previous steps.
 	 */
-	if (rename(".tmpconfig", autoconf_name))
+	if (rename(filename, autoconf_name))
 		return 1;
 
 	return 0;