dcavalca / rpms / rpm

Forked from rpms/rpm a year ago
Clone
Florian Festi b86572
From 89ce4e7ca592f5abafc3f25aeaa07d36a7b43a61 Mon Sep 17 00:00:00 2001
Florian Festi b86572
From: Panu Matilainen <pmatilai@redhat.com>
Florian Festi b86572
Date: Tue, 14 Nov 2023 11:37:48 +0200
Florian Festi b86572
Subject: [PATCH] Fix wrong return code on O_DIRECTORY open of invalid symlink
Florian Festi b86572
Florian Festi b86572
The dir argument to fsmOpenpath() is supposed to be a rough O_DIRECTORY
Florian Festi b86572
equivalent, and if the path is actually a misowned symlink it should
Florian Festi b86572
return ENOTDIR instead of ELOOP. Makes the resulting error messages
Florian Festi b86572
at least a little more comprehensible.
Florian Festi b86572
---
Florian Festi b86572
 lib/fsm.c | 5 +++--
Florian Festi b86572
 1 file changed, 3 insertions(+), 2 deletions(-)
Florian Festi b86572
Florian Festi b86572
diff --git a/lib/fsm.c b/lib/fsm.c
Florian Festi b86572
index 51f439ef3..091e90554 100644
Florian Festi b86572
--- a/lib/fsm.c
Florian Festi b86572
+++ b/lib/fsm.c
Florian Festi b86572
@@ -304,6 +304,7 @@ static int fsmOpenat(int dirfd, const char *path, int flags, int dir)
Florian Festi b86572
     struct stat lsb, sb;
Florian Festi b86572
     int sflags = flags | O_NOFOLLOW;
Florian Festi b86572
     int fd = openat(dirfd, path, sflags);
Florian Festi b86572
+    int ffd = fd;
Florian Festi b86572
 
Florian Festi b86572
     /*
Florian Festi b86572
      * Only ever follow symlinks by root or target owner. Since we can't
Florian Festi b86572
@@ -312,7 +313,7 @@ static int fsmOpenat(int dirfd, const char *path, int flags, int dir)
Florian Festi b86572
      * it could've only been the link owner or root.
Florian Festi b86572
      */
Florian Festi b86572
     if (fd < 0 && errno == ELOOP && flags != sflags) {
Florian Festi b86572
-	int ffd = openat(dirfd, path, flags);
Florian Festi b86572
+	ffd = openat(dirfd, path, flags);
Florian Festi b86572
 	if (ffd >= 0) {
Florian Festi b86572
 	    if (fstatat(dirfd, path, &lsb, AT_SYMLINK_NOFOLLOW) == 0) {
Florian Festi b86572
 		if (fstat(ffd, &sb) == 0) {
Florian Festi b86572
@@ -327,7 +328,7 @@ static int fsmOpenat(int dirfd, const char *path, int flags, int dir)
Florian Festi b86572
     }
Florian Festi b86572
 
Florian Festi b86572
     /* O_DIRECTORY equivalent */
Florian Festi b86572
-    if (dir && fd >= 0 && fstat(fd, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
Florian Festi b86572
+    if (dir && ((fd != ffd) || (fd >= 0 && fstat(fd, &sb) == 0 && !S_ISDIR(sb.st_mode)))) {
Florian Festi b86572
 	errno = ENOTDIR;
Florian Festi b86572
 	fsmClose(&fd;;
Florian Festi b86572
     }
Florian Festi b86572
-- 
Florian Festi b86572
2.43.0
Florian Festi b86572