Blob Blame History Raw
From fae45af368a90cdce95680d82b66d8e460ab939f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Wed, 23 Mar 2022 17:47:33 +0100
Subject: [PATCH] basic/stat-util: add null_or_empty_path_with_root()

(cherry picked from commit 48542eac39999f58f6c331b4b3cdf2d78bf15979)

Related: #2082131
---
 src/basic/stat-util.c     | 15 ++++++++++-----
 src/basic/stat-util.h     |  6 +++++-
 src/test/test-stat-util.c | 24 ++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
index efac7b002e..21e71794b4 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -127,17 +127,22 @@ bool null_or_empty(struct stat *st) {
         return false;
 }
 
-int null_or_empty_path(const char *fn) {
+int null_or_empty_path_with_root(const char *fn, const char *root) {
         struct stat st;
+        int r;
 
         assert(fn);
 
-        /* If we have the path, let's do an easy text comparison first. */
-        if (path_equal(fn, "/dev/null"))
+        /* A symlink to /dev/null or an empty file?
+         * When looking under root_dir, we can't expect /dev/ to be mounted,
+         * so let's see if the path is a (possibly dangling) symlink to /dev/null. */
+
+        if (path_equal_ptr(path_startswith(fn, root ?: "/"), "dev/null"))
                 return true;
 
-        if (stat(fn, &st) < 0)
-                return -errno;
+        r = chase_symlinks_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st, NULL);
+        if (r < 0)
+                return r;
 
         return null_or_empty(&st);
 }
diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h
index a566114f7c..2c5edeb891 100644
--- a/src/basic/stat-util.h
+++ b/src/basic/stat-util.h
@@ -31,9 +31,13 @@ static inline int dir_is_populated(const char *path) {
 }
 
 bool null_or_empty(struct stat *st) _pure_;
-int null_or_empty_path(const char *fn);
+int null_or_empty_path_with_root(const char *fn, const char *root);
 int null_or_empty_fd(int fd);
 
+static inline int null_or_empty_path(const char *fn) {
+        return null_or_empty_path_with_root(fn, NULL);
+}
+
 int path_is_read_only_fs(const char *path);
 
 int files_same(const char *filea, const char *fileb, int flags);
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
index 5f744b0288..9975a1848d 100644
--- a/src/test/test-stat-util.c
+++ b/src/test/test-stat-util.c
@@ -18,6 +18,30 @@
 #include "tests.h"
 #include "tmpfile-util.h"
 
+TEST(null_or_empty_path) {
+        assert_se(null_or_empty_path("/dev/null") == 1);
+        assert_se(null_or_empty_path("/dev/tty") == 1);  /* We assume that any character device is "empty", bleh. */
+        assert_se(null_or_empty_path("../../../../../../../../../../../../../../../../../../../../dev/null") == 1);
+        assert_se(null_or_empty_path("/proc/self/exe") == 0);
+        assert_se(null_or_empty_path("/nosuchfileordir") == -ENOENT);
+}
+
+TEST(null_or_empty_path_with_root) {
+        assert_se(null_or_empty_path_with_root("/dev/null", NULL) == 1);
+        assert_se(null_or_empty_path_with_root("/dev/null", "/") == 1);
+        assert_se(null_or_empty_path_with_root("/dev/null", "/.././../") == 1);
+        assert_se(null_or_empty_path_with_root("/dev/null", "/.././..") == 1);
+        assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", NULL) == 1);
+        assert_se(null_or_empty_path_with_root("../../../../../../../../../../../../../../../../../../../../dev/null", "/") == 1);
+        assert_se(null_or_empty_path_with_root("/proc/self/exe", NULL) == 0);
+        assert_se(null_or_empty_path_with_root("/proc/self/exe", "/") == 0);
+        assert_se(null_or_empty_path_with_root("/nosuchfileordir", NULL) == -ENOENT);
+        assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././../") == -ENOENT);
+        assert_se(null_or_empty_path_with_root("/nosuchfileordir", "/.././..") == -ENOENT);
+        assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar") == 1);
+        assert_se(null_or_empty_path_with_root("/foobar/barbar/dev/null", "/foobar/barbar/") == 1);
+}
+
 TEST(files_same) {
         _cleanup_close_ int fd = -1;
         char name[] = "/tmp/test-files_same.XXXXXX";