Skip to contentSkip to conversation

landlock: Handle weird files

PATCH In review Mickaël Salaün 4 participants 7 messages Jan 10, 2025 – Jan 15, 2025

Subsystems (3)
lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

PATCH 1/2 landlock: Handle weird files

A corrupted filesystem (e.g. bcachefs) might return weird files. Instead of throwing a warning and allowing access to such file, treat them as regular files.

---
 security/landlock/fs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
security/landlock/fs.c +5 -6
index e31b97a9f175..7adb25150488 100644
@@ -937,10 +937,6 @@ static access_mask_t get_mode_access(const umode_t mode)
 	switch (mode & S_IFMT) {
 	case S_IFLNK:
 		return LANDLOCK_ACCESS_FS_MAKE_SYM;
-	case 0:
-		/* A zero mode translates to S_IFREG. */
-	case S_IFREG:
-		return LANDLOCK_ACCESS_FS_MAKE_REG;
 	case S_IFDIR:
 		return LANDLOCK_ACCESS_FS_MAKE_DIR;
 	case S_IFCHR:
@@ -951,9 +947,12 @@ static access_mask_t get_mode_access(const umode_t mode)
 		return LANDLOCK_ACCESS_FS_MAKE_FIFO;
 	case S_IFSOCK:
 		return LANDLOCK_ACCESS_FS_MAKE_SOCK;
+	case S_IFREG:
+	case 0:
+		/* A zero mode translates to S_IFREG. */
 	default:
-		WARN_ON_ONCE(1);
-		return 0;
+		/* Treats weird files as regular files. */
+		return LANDLOCK_ACCESS_FS_MAKE_REG;
 	}
 }
 
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH 2/2 landlock: Constify get_mode_access()

Use __attribute_const__ for get_mode_access().

---
 security/landlock/fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/landlock/fs.c +1 -1
index 7adb25150488..f81d0335b825 100644
@@ -932,7 +932,7 @@ static int current_check_access_path(const struct path *const path,
 	return check_access_path(dom, path, access_request);
 }
 
-static access_mask_t get_mode_access(const umode_t mode)
+static __attribute_const__ access_mask_t get_mode_access(const umode_t mode)
 {
 	switch (mode & S_IFMT) {
 	case S_IFLNK:
Signature · 1 line
2.47.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH v1 2/2] landlock: Constify get_mode_access()

Quoted 24 lines · Mickaël Salaün

Use __attribute_const__ for get_mode_access().

---
 security/landlock/fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/landlock/fs.c +1 -1
index 7adb25150488..f81d0335b825 100644
@@ -932,7 +932,7 @@ static int current_check_access_path(const struct path *const path,
 	return check_access_path(dom, path, access_request);
 }
 
-static access_mask_t get_mode_access(const umode_t mode)
+static __attribute_const__ access_mask_t get_mode_access(const umode_t mode)
 {
 	switch (mode & S_IFMT) {
 	case S_IFLNK:
Signature · 1 line
2.47.1
Reviewed-by on v1 1/2
#3 lore raw
In reply to Mickaël Salaün
Quoted 53 lines · Mickaël Salaün

A corrupted filesystem (e.g. bcachefs) might return weird files. Instead of throwing a warning and allowing access to such file, treat them as regular files.

---
 security/landlock/fs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
security/landlock/fs.c +5 -6
index e31b97a9f175..7adb25150488 100644
@@ -937,10 +937,6 @@ static access_mask_t get_mode_access(const umode_t mode)
 	switch (mode & S_IFMT) {
 	case S_IFLNK:
 		return LANDLOCK_ACCESS_FS_MAKE_SYM;
-	case 0:
-		/* A zero mode translates to S_IFREG. */
-	case S_IFREG:
-		return LANDLOCK_ACCESS_FS_MAKE_REG;
 	case S_IFDIR:
 		return LANDLOCK_ACCESS_FS_MAKE_DIR;
 	case S_IFCHR:
@@ -951,9 +947,12 @@ static access_mask_t get_mode_access(const umode_t mode)
 		return LANDLOCK_ACCESS_FS_MAKE_FIFO;
 	case S_IFSOCK:
 		return LANDLOCK_ACCESS_FS_MAKE_SOCK;
+	case S_IFREG:
+	case 0:
+		/* A zero mode translates to S_IFREG. */
 	default:
-		WARN_ON_ONCE(1);
-		return 0;
+		/* Treats weird files as regular files. */
+		return LANDLOCK_ACCESS_FS_MAKE_REG;
 	}
 }
 
Signature · 1 line
2.47.1

Makes sense to me, since this is enforcing a stronger check than before and can only happen in the case of corruption.

I do not have a good intuition about what happens afterwards when the file system is in such a state. I imagine that this will usually give an error shortly afterwards, as the opening of the file continues? Is that right?

–Günther

author maintainer
#4 lore raw
Quoted 57 lines · Günther Noack
Quoted 53 lines · Mickaël Salaün

A corrupted filesystem (e.g. bcachefs) might return weird files. Instead of throwing a warning and allowing access to such file, treat them as regular files.

---
 security/landlock/fs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
security/landlock/fs.c +5 -6
index e31b97a9f175..7adb25150488 100644
@@ -937,10 +937,6 @@ static access_mask_t get_mode_access(const umode_t mode)
 	switch (mode & S_IFMT) {
 	case S_IFLNK:
 		return LANDLOCK_ACCESS_FS_MAKE_SYM;
-	case 0:
-		/* A zero mode translates to S_IFREG. */
-	case S_IFREG:
-		return LANDLOCK_ACCESS_FS_MAKE_REG;
 	case S_IFDIR:
 		return LANDLOCK_ACCESS_FS_MAKE_DIR;
 	case S_IFCHR:
@@ -951,9 +947,12 @@ static access_mask_t get_mode_access(const umode_t mode)
 		return LANDLOCK_ACCESS_FS_MAKE_FIFO;
 	case S_IFSOCK:
 		return LANDLOCK_ACCESS_FS_MAKE_SOCK;
+	case S_IFREG:
+	case 0:
+		/* A zero mode translates to S_IFREG. */
 	default:
-		WARN_ON_ONCE(1);
-		return 0;
+		/* Treats weird files as regular files. */
+		return LANDLOCK_ACCESS_FS_MAKE_REG;
 	}
 }
 
Signature · 1 line
2.47.1
Quoted 7 lines

Makes sense to me, since this is enforcing a stronger check than before and can only happen in the case of corruption.

I do not have a good intuition about what happens afterwards when the file system is in such a state. I imagine that this will usually give an error shortly afterwards, as the opening of the file continues? Is that right?

I guess it depends on the filesystem implementation. For instance, XFS returns an error if a weird file is detected [1], whereas bcachefs ignores it (which is considered a bug, but not fixed yet) [2].

[1] https://lore.kernel.org/all/Zpc46HEacI%[email protected]/
[2] https://lore.kernel.org/all/4hohnthh54adx35lnxzedop3oxpntpmtygxso4iraiexfdlt4d@6m7ssepvjyar/

Quoted 3 lines

–Günther

#5 lore raw
Quoted 3 lines · Mickaël Salaün

I guess it depends on the filesystem implementation. For instance, XFS returns an error if a weird file is detected [1], whereas bcachefs ignores it (which is considered a bug, but not fixed yet) [2].

If a filesyste, returns an invalid mode that's a file system bug and needs to be fixed there. Warning in a consumer is perfectly fine. But the right action in that case is indeed not to grant the access.

#6 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Mickaël Salaün

I guess it depends on the filesystem implementation. For instance, XFS returns an error if a weird file is detected [1], whereas bcachefs ignores it (which is considered a bug, but not fixed yet) [2].

If a filesyste, returns an invalid mode that's a file system bug and needs to be fixed there. Warning in a consumer is perfectly fine. But the right action in that case is indeed not to grant the access.

Fyi, anonymous inodes traditionally set the mode to 0 which is really annoying:

lrwx------ 1 root root 64 15. Jan 11:52 94 -> anon_inode:bpf-prog

Quoted 1 line

sudo stat -L /proc/1/fd/94

File: /proc/1/fd/94
Size: 0 Blocks: 0 IO Block: 4096 weird file
Device: 0,15 Inode: 4120 Links: 1
Access: (0600/?rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-11-05 17:15:54.404000000 +0100
Modify: 2024-11-05 17:15:54.404000000 +0100
Change: 2024-11-05 17:15:54.404000000 +0100
Birth: -