git.iko.soy/typeable/nixpkgs
on master help sign in
Merge branch 'setuid-wrapper-readlink'
commit6dab907ebe9c8015b8cbc4871313ed48c64c548c authorShea Levy <shea@shealevy.com> dateWed 7 Mar 2018, 12:44 (9 years ago) parenteab479adffa3d2
1 file changed+25−11
nixos/modules/security/wrappers/wrapper.c+25−11
nixos/modules/security/wrappers/wrapper.c+25−11
@@ -1,3 +1,4 @@
1+#define _GNU_SOURCE
12#include <stdlib.h>
23#include <stdio.h>
34#include <string.h>
@@ -161,22 +162,34 @@ static int make_caps_ambient(const char *selfPath)
161162
162163int main(int argc, char * * argv)
163164{
164− // I *think* it's safe to assume that a path from a symbolic link
165− // should safely fit within the PATH_MAX system limit. Though I'm
166− // not positive it's safe...
167− char selfPath[PATH_MAX];
168− int selfPathSize = readlink("/proc/self/exe", selfPath, sizeof(selfPath));
165+ // O_PATH | O_NOFOLLOW gives us a fd pointing to the symlink
166+ int selfExe = open("/proc/self/exe", O_PATH | O_CLOEXEC | O_NOFOLLOW);
167+ assert(selfExe != -1);
168+ struct stat st;
169+ assert(fstat(selfExe, &st) != -1);
170+ size_t selfPathCap = st.st_size + 1;
171+ char *selfPath = malloc(selfPathCap);
172+ assert(selfPath);
173+ int selfPathSize = readlinkat(selfExe, "", selfPath, selfPathCap);
169174
170175 assert(selfPathSize > 0);
171176
172177 // Assert we have room for the zero byte, this ensures the path
173178 // isn't being truncated because it's too big for the buffer.
174179 //
175− // A better way to handle this might be to use something like the
176− // whereami library (https://github.com/gpakosz/whereami) or a
177− // loop that resizes the buffer and re-reads the link if the
178− // contents are being truncated.
179− assert(selfPathSize < sizeof(selfPath));
180+ // selfPathSize is the number of bytes readlinkat put into the
181+ // buffer, which does *not* append a null byte. selfPathCap is the
182+ // capacity of the buffer, which was set to the number of bytes in
183+ // the link contents (again, without the null byte) plus one for
184+ // the null byte.
185+ //
186+ // I don't think it's possible for the link contents to change
187+ // between opening a symlink fd and readlinkat on it, so this is
188+ // probably not necessary. Doubly so since this is /proc/self/exe,
189+ // not a normal symlink. But it's trivial to check.
190+ assert(selfPathSize < selfPathCap);
191+
192+ assert(close(selfExe));
180193
181194 // Set the zero byte since readlink doesn't do that for us.
182195 selfPath[selfPathSize] = '\0';
@@ -197,7 +210,6 @@ int main(int argc, char * * argv)
197210 // `selfPath', and not, say, as some other setuid program. That
198211 // is, our effective uid/gid should match the uid/gid of
199212 // `selfPath'.
200− struct stat st;
201213 assert(lstat(selfPath, &st) != -1);
202214
203215 assert(!(st.st_mode & S_ISUID) || (st.st_uid == geteuid()));
@@ -228,6 +240,8 @@ int main(int argc, char * * argv)
228240 // capabilities too!
229241 make_caps_ambient(selfPath);
230242
243+ free(selfPath);
244+
231245 execve(sourceProg, argv, environ);
232246
233247 fprintf(stderr, "%s: cannot run `%s': %s\n",