| 1 |
From: Brian May <[email protected]> |
| 2 |
Date: Thu, 8 Aug 2024 10:43:18 +1000 |
| 3 |
Subject: add-include-support-2 |
| 4 |
|
| 5 |
commit 0259f1c44927ab8f5906212804693dec48c9a04a |
| 6 |
Author: Nicolas Williams <[email protected]> |
| 7 |
Date: Thu Mar 9 14:21:17 2017 -0600 |
| 8 |
|
| 9 |
Fix CR comments on include/includedir |
| 10 |
|
| 11 |
Origin: upstream, https://github.com/heimdal/heimdal/commit/0259f1c44927ab8f5906212804693dec48c9a04a |
| 12 |
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1074775 |
| 13 |
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/heimdal/+bug/2037321 |
| 14 |
Last-Update: 2024-07-05 |
| 15 |
--- |
| 16 |
lib/krb5/config_file.c | 107 ++++++++++++++++++++++++++++++++++++++++--------- |
| 17 |
1 file changed, 89 insertions(+), 18 deletions(-) |
| 18 |
|
| 19 |
diff --git a/lib/krb5/config_file.c b/lib/krb5/config_file.c |
| 20 |
index 3c4b0b6..947c302 100644 |
| 21 |
--- a/lib/krb5/config_file.c |
| 22 |
+++ b/lib/krb5/config_file.c |
| 23 |
@@ -337,6 +337,41 @@ parse_plist_config(krb5_context context, const char *path, krb5_config_section * |
| 24 |
|
| 25 |
#endif |
| 26 |
|
| 27 |
+static int |
| 28 |
+is_absolute_path(const char *path) |
| 29 |
+{ |
| 30 |
+ /* |
| 31 |
+ * An absolute path is one that refers to an explicit object |
| 32 |
+ * without ambiguity. |
| 33 |
+ */ |
| 34 |
+#ifdef WIN32 |
| 35 |
+ size_t len = strlen(path); |
| 36 |
+ |
| 37 |
+ /* UNC path is by definition absolute */ |
| 38 |
+ if (len > 2 |
| 39 |
+ && ISPATHSEP(path[0]) |
| 40 |
+ && ISPATHSEP(path[1])) |
| 41 |
+ return 1; |
| 42 |
+ |
| 43 |
+ /* A drive letter path might be absolute */ |
| 44 |
+ if (len > 3 |
| 45 |
+ && isalpha(path[0]) |
| 46 |
+ && path[1] == ':' |
| 47 |
+ && ISPATHSEP(path[2])) |
| 48 |
+ return 1; |
| 49 |
+ |
| 50 |
+ /* |
| 51 |
+ * if no drive letter but first char is a path |
| 52 |
+ * separator then the drive letter must be obtained |
| 53 |
+ * from the including file. |
| 54 |
+ */ |
| 55 |
+#else |
| 56 |
+ /* UNIX is easy, first char '/' is absolute */ |
| 57 |
+ if (ISPATHSEP(path[0])) |
| 58 |
+ return 1; |
| 59 |
+#endif |
| 60 |
+ return 0; |
| 61 |
+} |
| 62 |
|
| 63 |
/* |
| 64 |
* Parse the config file `fname', generating the structures into `res' |
| 65 |
@@ -380,6 +415,12 @@ krb5_config_parse_debug (struct fileptr *f, |
| 66 |
p += sizeof("include"); |
| 67 |
while (isspace(*p)) |
| 68 |
p++; |
| 69 |
+ if (!is_absolute_path(p)) { |
| 70 |
+ krb5_set_error_message(f->context, EINVAL, |
| 71 |
+ "Configuration include path must be " |
| 72 |
+ "absolute"); |
| 73 |
+ return EINVAL; |
| 74 |
+ } |
| 75 |
ret = krb5_config_parse_file_multi(f->context, p, res); |
| 76 |
if (ret) |
| 77 |
return ret; |
| 78 |
@@ -388,6 +429,12 @@ krb5_config_parse_debug (struct fileptr *f, |
| 79 |
p += sizeof("includedir"); |
| 80 |
while (isspace(*p)) |
| 81 |
p++; |
| 82 |
+ if (!is_absolute_path(p)) { |
| 83 |
+ krb5_set_error_message(f->context, EINVAL, |
| 84 |
+ "Configuration includedir path must be " |
| 85 |
+ "absolute"); |
| 86 |
+ return EINVAL; |
| 87 |
+ } |
| 88 |
ret = krb5_config_parse_dir_multi(f->context, p, res); |
| 89 |
if (ret) |
| 90 |
return ret; |
| 91 |
@@ -451,7 +498,14 @@ krb5_config_parse_dir_multi(krb5_context context, |
| 92 |
int is_valid = 1; |
| 93 |
|
| 94 |
while (*p) { |
| 95 |
- if (!isalpha(*p) && *p != '_' && *p != '-' && |
| 96 |
+ /* |
| 97 |
+ * Here be dragons. The call to krb5_config_parse_file_multi() |
| 98 |
+ * below expands path tokens. Because of the limitations here |
| 99 |
+ * on file naming, we can't have path tokens in the file name, |
| 100 |
+ * so we're safe. Anyone changing this if condition here should |
| 101 |
+ * be aware. |
| 102 |
+ */ |
| 103 |
+ if (!isalnum(*p) && *p != '_' && *p != '-' && |
| 104 |
strcmp(p, ".conf") != 0) { |
| 105 |
is_valid = 0; |
| 106 |
break; |
| 107 |
@@ -462,13 +516,17 @@ krb5_config_parse_dir_multi(krb5_context context, |
| 108 |
continue; |
| 109 |
|
| 110 |
if (asprintf(&path, "%s/%s", dname, entry->d_name) == -1 || |
| 111 |
- path == NULL) |
| 112 |
+ path == NULL) { |
| 113 |
+ (void) closedir(d); |
| 114 |
return krb5_enomem(context); |
| 115 |
+ } |
| 116 |
ret = krb5_config_parse_file_multi(context, path, res); |
| 117 |
free(path); |
| 118 |
- if (ret == ENOMEM) |
| 119 |
+ if (ret == ENOMEM) { |
| 120 |
+ (void) closedir(d); |
| 121 |
return krb5_enomem(context);; |
| 122 |
- /* Ignore malformed config files */ |
| 123 |
+ } |
| 124 |
+ /* Ignore malformed config files so we don't lock out admins, etc... */ |
| 125 |
} |
| 126 |
(void) closedir(d); |
| 127 |
return 0; |
| 128 |
@@ -497,6 +555,7 @@ krb5_config_parse_file_multi (krb5_context context, |
| 129 |
unsigned lineno = 0; |
| 130 |
krb5_error_code ret; |
| 131 |
struct fileptr f; |
| 132 |
+ struct stat st; |
| 133 |
|
| 134 |
if (context->config_include_depth > 5) { |
| 135 |
krb5_warnx(context, "Maximum config file include depth reached; " |
| 136 |
@@ -550,14 +609,13 @@ krb5_config_parse_file_multi (krb5_context context, |
| 137 |
} |
| 138 |
|
| 139 |
if (is_plist_file(fname)) { |
| 140 |
+ context->config_include_depth--; |
| 141 |
#ifdef __APPLE__ |
| 142 |
ret = parse_plist_config(context, fname, res); |
| 143 |
- context->config_include_depth--; |
| 144 |
if (ret) { |
| 145 |
krb5_set_error_message(context, ret, |
| 146 |
"Failed to parse plist %s", fname); |
| 147 |
- if (newfname) |
| 148 |
- free(newfname); |
| 149 |
+ free(newfname); |
| 150 |
return ret; |
| 151 |
} |
| 152 |
#else |
| 153 |
@@ -569,40 +627,53 @@ krb5_config_parse_file_multi (krb5_context context, |
| 154 |
#ifdef KRB5_USE_PATH_TOKENS |
| 155 |
char * exp_fname = NULL; |
| 156 |
|
| 157 |
+ /* |
| 158 |
+ * Note that krb5_config_parse_dir_multi() doesn't want tokens |
| 159 |
+ * expanded here, but it happens to limit the names of files to |
| 160 |
+ * include such that there can be no tokens to expand. Don't |
| 161 |
+ * add token expansion for tokens using _, say. |
| 162 |
+ */ |
| 163 |
ret = _krb5_expand_path_tokens(context, fname, 1, &exp_fname); |
| 164 |
if (ret) { |
| 165 |
context->config_include_depth--; |
| 166 |
- if (newfname) |
| 167 |
- free(newfname); |
| 168 |
+ free(newfname); |
| 169 |
return ret; |
| 170 |
} |
| 171 |
|
| 172 |
- if (newfname) |
| 173 |
- free(newfname); |
| 174 |
+ free(newfname); |
| 175 |
fname = newfname = exp_fname; |
| 176 |
#endif |
| 177 |
|
| 178 |
f.context = context; |
| 179 |
f.f = fopen(fname, "r"); |
| 180 |
f.s = NULL; |
| 181 |
- if(f.f == NULL) { |
| 182 |
+ if (f.f == NULL || fstat(fileno(f.f), &st) == -1) { |
| 183 |
+ if (f.f != NULL) |
| 184 |
+ (void) fclose(f.f); |
| 185 |
context->config_include_depth--; |
| 186 |
ret = errno; |
| 187 |
- krb5_set_error_message (context, ret, "open %s: %s", |
| 188 |
- fname, strerror(ret)); |
| 189 |
- if (newfname) |
| 190 |
- free(newfname); |
| 191 |
+ krb5_set_error_message(context, ret, "open or stat %s: %s", |
| 192 |
+ fname, strerror(ret)); |
| 193 |
+ free(newfname); |
| 194 |
return ret; |
| 195 |
} |
| 196 |
|
| 197 |
+ if (!S_ISREG(st.st_mode)) { |
| 198 |
+ (void) fclose(f.f); |
| 199 |
+ context->config_include_depth--; |
| 200 |
+ free(newfname); |
| 201 |
+ krb5_set_error_message(context, EISDIR, "not a regular file %s: %s", |
| 202 |
+ fname, strerror(EISDIR)); |
| 203 |
+ return EISDIR; |
| 204 |
+ } |
| 205 |
+ |
| 206 |
ret = krb5_config_parse_debug (&f, res, &lineno, &str); |
| 207 |
context->config_include_depth--; |
| 208 |
fclose(f.f); |
| 209 |
if (ret) { |
| 210 |
krb5_set_error_message (context, ret, "%s:%u: %s", |
| 211 |
fname, lineno, str); |
| 212 |
- if (newfname) |
| 213 |
- free(newfname); |
| 214 |
+ free(newfname); |
| 215 |
return ret; |
| 216 |
} |
| 217 |
} |