chiark / gitweb /
path-util: introduce new safe_getcwd() wrapper
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Jan 2018 10:16:31 +0000 (11:16 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:50:10 +0000 (07:50 +0200)
It's like get_current_dir_name() but protects us from
CVE-2018-1000001-style exploits:

https://www.halfdog.net/Security/2017/LibcRealpathBufferUnderflow/

src/basic/path-util.c
src/basic/path-util.h

index 2be77126c126ca4a0d9df99ad62aeaea567a997b..97cb5046a1a7ab0cb31e6f09f751d5b15e35a3e1 100644 (file)
@@ -92,6 +92,24 @@ char *path_make_absolute(const char *p, const char *prefix) {
 }
 #endif // 0
 
+int safe_getcwd(char **ret) {
+        char *cwd;
+
+        cwd = get_current_dir_name();
+        if (!cwd)
+                return negative_errno();
+
+        /* Let's make sure the directory is really absolute, to protect us from the logic behind
+         * CVE-2018-1000001 */
+        if (cwd[0] != '/') {
+                free(cwd);
+                return -ENOMEDIUM;
+        }
+
+        *ret = cwd;
+        return 0;
+}
+
 int path_make_absolute_cwd(const char *p, char **ret) {
         char *c;
 
index 38e6c927c1b850226f6697cd30e31199f4bcfa70..9faa128e3747fa3b48140cf04c2a8589efe43604 100644 (file)
@@ -45,6 +45,7 @@ bool path_is_absolute(const char *p) _pure_;
 #if 0 /// UNNEEDED by elogind
 char* path_make_absolute(const char *p, const char *prefix);
 #endif // 0
+int safe_getcwd(char **ret);
 int path_make_absolute_cwd(const char *p, char **ret);
 #if 0 /// UNNEEDED by elogind
 int path_make_relative(const char *from_dir, const char *to_path, char **_r);