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