594167
From fae45af368a90cdce95680d82b66d8e460ab939f Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Wed, 23 Mar 2022 17:47:33 +0100
594167
Subject: [PATCH] basic/stat-util: add null_or_empty_path_with_root()
594167
594167
(cherry picked from commit 48542eac39999f58f6c331b4b3cdf2d78bf15979)
594167
594167
Related: #2082131
594167
---
594167
 src/basic/stat-util.c     | 15 ++++++++++-----
594167
 src/basic/stat-util.h     |  6 +++++-
594167
 src/test/test-stat-util.c | 24 ++++++++++++++++++++++++
594167
 3 files changed, 39 insertions(+), 6 deletions(-)
594167
594167
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
594167
index efac7b002e..21e71794b4 100644
594167
--- a/src/basic/stat-util.c
594167
+++ b/src/basic/stat-util.c
594167
@@ -127,17 +127,22 @@ bool null_or_empty(struct stat *st) {
594167
         return false;
594167
 }
594167
 
594167
-int null_or_empty_path(const char *fn) {
594167
+int null_or_empty_path_with_root(const char *fn, const char *root) {
594167
         struct stat st;
594167
+        int r;
594167
 
594167
         assert(fn);
594167
 
594167
-        /* If we have the path, let's do an easy text comparison first. */
594167
-        if (path_equal(fn, "/dev/null"))
594167
+        /* A symlink to /dev/null or an empty file?
594167
+         * When looking under root_dir, we can't expect /dev/ to be mounted,
594167
+         * so let's see if the path is a (possibly dangling) symlink to /dev/null. */
594167
+
594167
+        if (path_equal_ptr(path_startswith(fn, root ?: "/"), "dev/null"))
594167
                 return true;
594167
 
594167
-        if (stat(fn, &st) < 0)
594167
-                return -errno;
594167
+        r = chase_symlinks_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st, NULL);
594167
+        if (r < 0)
594167
+                return r;
594167
 
594167
         return null_or_empty(&st);
594167
 }
594167
diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h
594167
index a566114f7c..2c5edeb891 100644
594167
--- a/src/basic/stat-util.h
594167
+++ b/src/basic/stat-util.h
594167
@@ -31,9 +31,13 @@ static inline int dir_is_populated(const char *path) {
594167
 }
594167
 
594167
 bool null_or_empty(struct stat *st) _pure_;
594167
-int null_or_empty_path(const char *fn);
594167
+int null_or_empty_path_with_root(const char *fn, const char *root);
594167
 int null_or_empty_fd(int fd);
594167
 
594167
+static inline int null_or_empty_path(const char *fn) {
594167
+        return null_or_empty_path_with_root(fn, NULL);
594167
+}
594167
+
594167
 int path_is_read_only_fs(const char *path);
594167
 
594167
 int files_same(const char *filea, const char *fileb, int flags);
594167
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
594167
index 5f744b0288..9975a1848d 100644
594167
--- a/src/test/test-stat-util.c
594167
+++ b/src/test/test-stat-util.c
594167
@@ -18,6 +18,30 @@
594167
 #include "tests.h"
594167
 #include "tmpfile-util.h"
594167
 
594167
+TEST(null_or_empty_path) {
594167
+        assert_se(null_or_empty_path("/dev/null") == 1);
594167
+        assert_se(null_or_empty_path("/dev/tty") == 1);  /* We assume that any character device is "empty", bleh. */
594167
+        assert_se(null_or_empty_path("../../../../../../../../../../../../../../../../../../../../dev/null") == 1);
594167
+        assert_se(null_or_empty_path("/proc/self/exe") == 0);
594167
+        assert_se(null_or_empty_path("/nosuchfileordir") == -ENOENT);
594167
+}
594167
+
594167
+TEST(null_or_empty_path_with_root) {
594167
+        assert_se(null_or_empty_path_with_root("/dev/null", NULL) == 1);
594167
+        assert_se(null_or_empty_path_with_root("/dev/null", "/") == 1);
594167
+        assert_se(null_or_empty_path_with_root("/dev/null", "/.././../") == 1);
594167
+        assert_se(null_or_empty_path_with_root("/dev/null", "/.././..") == 1);
594167
+        assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", NULL) == 1);
594167
+        assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", "/") == 1);
594167
+        assert_se(null_or_empty_path_with_root("/proc/self/exe", NULL) == 0);
594167
+        assert_se(null_or_empty_path_with_root("/proc/self/exe", "/") == 0);
594167
+        assert_se(null_or_empty_path_with_root("/nosuchfileordir", NULL) == -ENOENT);
594167
+        assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././../") == -ENOENT);
594167
+        assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././..") == -ENOENT);
594167
+        assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar") == 1);
594167
+        assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar/") == 1);
594167
+}
594167
+
594167
 TEST(files_same) {
594167
         _cleanup_close_ int fd = -1;
594167
         char name[] = "/tmp/test-files_same.XXXXXX";