diff mbox series

[v5.4-v4.19,4/6] fs: new helper vfs_empty_path()

Message ID 20240918-statx-stable-linux-5-4-y-v1-4-8a771c9bbe5f@gmail.com (mailing list archive)
State New
Headers show
Series Backport statx(..., NULL, AT_EMPTY_PATH, ...) | expand

Commit Message

Miao Wang via B4 Relay Sept. 18, 2024, 2:12 p.m. UTC
From: Christian Brauner <brauner@kernel.org>

commit 1bc6d44 upstream.

Make it possible to quickly check whether AT_EMPTY_PATH is valid.
Note, after some discussion we decided to also allow NULL to be passed
instead of requiring the empty string.

Signed-off-by: Christian Brauner <brauner@kernel.org>

Cc: <stable@vger.kernel.org> # 4.19.x-5.4.x
Signed-off-by: Miao Wang <shankerwangmiao@gmail.com>
Tested-by: Xi Ruoyao <xry111@xry111.site>
---
 fs/stat.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
diff mbox series

Patch

diff --git a/fs/stat.c b/fs/stat.c
index 526fa0801cad..3ae958308e48 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -150,6 +150,23 @@  int vfs_statx_fd(unsigned int fd, struct kstat *stat,
 }
 EXPORT_SYMBOL(vfs_statx_fd);
 
+static inline bool vfs_empty_path(int dfd, const char __user *path)
+{
+	char c;
+
+	if (dfd < 0)
+		return false;
+
+	/* We now allow NULL to be used for empty path. */
+	if (!path)
+		return true;
+
+	if (unlikely(get_user(c, path)))
+		return false;
+
+	return !c;
+}
+
 /**
  * vfs_statx - Get basic and extra attributes by filename
  * @dfd: A file descriptor representing the base dir for a relative filename