Skip to contentSkip to conversation

Add pending patches to master

PATCH In review Mark Hatle 2 participants 5 messages Apr 24 – Apr 25

lore
Share & tools
lore
author
#1 lore raw

PATCH 1/3 Only copy xattrs on a rename if it's cross-filesystem

In reply to Mark Hatle

From: Daniel Burgener <[email protected]>

On same file-system renames, the path update in the db means that the
xattrs are already present, and copying results in duplicates. This was
previously masked by the bug fixed in d1db9c219abf92f15303486a409292237f1fc790, since that prevented the xattr copying. Now that that bug is fixed, we started observing this
issue in 1.9.3.

AI-Generated: Code fix by Claude Haiku 4.5. Test code by me.

---
 pseudo_db.c        |  4 +++-
 test/test-xattr.sh | 12 ++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
pseudo_db.c +3 -1
index 9713e0c..6a33f20 100644
@@ -2196,7 +2196,9 @@ pdb_update_inode(pseudo_msg_t *msg) {
 	found_existing = !pdb_find_file_path(oldmsg);
 	if (found_existing) {
 		/* we have an existing file entry */
-		pdb_copy_xattrs(oldmsg, msg);
+		if (oldmsg->dev != msg->dev || oldmsg->ino != msg->ino) {
+			pdb_copy_xattrs(oldmsg, msg);
+		}
 	}
 	sqlite3_bind_int(update, 1, msg->dev);
 	sqlite3_bind_int64(update, 2, signed_ino(msg->ino));
test/test-xattr.sh +12 -0
index 6ac6b3c..89fbd35 100755
@@ -65,6 +65,18 @@ then
     exit 1
 fi
 
+mv f1 f2
+attrs=`getfattr -d -m - f2 | grep -v '^#'`
+expected=$'security.dummy="test_f2"\nuser.dummy="test_f1"'
+if [ "$attrs" != "$expected" ]
+then
+	#echo "Fail, unpexected getfattr result '$attrs'"
+	rm -f f1
+	rm -f f2
+	exit 1
+fi
+
 #echo "Passed."
 rm -f f1
+rm -f f2
 exit 0
Signature · 1 line
1.8.3.1

Mark Hatle

author
#0 lore raw

cover Add pending patches to master

This includes the two pending patches to pseduo and a test case for Peter's issue so hopefully it doesn't happen again in the future.

These changes are currently applied to 'master-next'. Assuming nobody finds issues with these, I'll merge to master and bump the version (1.9.6) on Monday.

(RP if you think the build needs it before Monday, go ahead! I've tested everything and it looks safe to me.)

Daniel Burgener (1):
Only copy xattrs on a rename if it's cross-filesystem

Mark Hatle (1):
test: Add test for linkat chroot path stripping

Peter Kjellerstedt (1):
linkat: Avoid a segmentation fault

 ports/unix/guts/linkat.c   |  2 +-
 pseudo_db.c                |  4 ++-
 test/test-linkat-chroot.c  | 69 ++++++++++++++++++++++++++++++++++++++++++++++
 test/test-linkat-chroot.sh | 19 +++++++++++++
 test/test-xattr.sh         | 12 ++++++++
 5 files changed, 104 insertions(+), 2 deletions(-)
 create mode 100644 test/test-linkat-chroot.c
 create mode 100755 test/test-linkat-chroot.sh
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/3 linkat: Avoid a segmentation fault

From: Peter Kjellerstedt <[email protected]>

This avoids the following segmentation fault (in useradd):

Program terminated with signal SIGSEGV, Segmentation fault. #0 0x0000795b22562f49 in wrap_linkat (olddirfd=olddirfd@entry=-100,

      oldname=oldname@entry=0x6016835f1fb0 "/etc/group.3167327",
      newdirfd=newdirfd@entry=-100,
      newname=newname@entry=0x6016835f1fd0 "/etc/group.lock",
      flags=flags@entry=0) at ports/unix/guts/linkat.c:37
  37              oldpath = oldname;
  (gdb) bt
  #0  0x0000795b22562f49 in wrap_linkat (olddirfd=olddirfd@entry=-100,
      oldname=oldname@entry=0x6016835f1fb0 "/etc/group.3167327",
      newdirfd=newdirfd@entry=-100,
      newname=newname@entry=0x6016835f1fd0 "/etc/group.lock",
      flags=flags@entry=0) at ports/unix/guts/linkat.c:37
  #1  0x0000795b2257b95c in wrap_link (newname=0x6016835f1fd0 "/etc/group.lock",
      oldname=0x6016835f1fb0 "/etc/group.3167327") at ports/unix/guts/link.c:17
  #2  link (oldname=0x6016835f1fb0 "/etc/group.3167327",
      newname=0x6016835f1fd0 "/etc/group.lock") at pseudo_wrapfuncs.c:8968
  #3  0x00006016814d45dd in do_lock_file (file=file@entry=0x6016835f1fb0 "/etc/group.3167327",
      lock=lock@entry=0x6016835f1fd0 "/etc/group.lock", log=log@entry=true)
      at ../../sources/shadow-4.19.4/lib/commonio.c:167
  #4  0x00006016814d4e88 in commonio_lock_nowait (db=db@entry=0x6016814df9e0 <group_db>,
      log=log@entry=true) at ../../sources/shadow-4.19.4/lib/commonio.c:373
  #5  0x00006016814d4f09 in commonio_lock (db=db@entry=0x6016814df9e0 <group_db>)
      at ../../sources/shadow-4.19.4/lib/commonio.c:413
  #6  0x00006016814cfc9a in gr_lock () at ../../sources/shadow-4.19.4/lib/groupio.c:141
  #7  0x00006016814c981f in open_group_files (process_selinux=process_selinux@entry=false)
      at ../../sources/shadow-4.19.4/src/useradd.c:1797
  #8  0x00006016814cacdc in open_files (process_selinux=process_selinux@entry=false)
      at ../../sources/shadow-4.19.4/src/useradd.c:1759
  #9  0x00006016814cc388 in main (argc=<optimized out>, argv=<optimized out>)
      at ../../sources/shadow-4.19.4/src/useradd.c:2585
  (gdb) l
  32              if (olddirfd != AT_FDCWD || newdirfd != AT_FDCWD) {
  33                      errno = ENOSYS;
  34                      return -1;
  35              }
  36      #endif
  37              oldpath = oldname;
  38              if (pseudo_chroot_len && strncmp(oldpath, pseudo_chroot, pseudo_chroot_len) &&
  39                      oldpath[pseudo_chroot_len] == '/') {
  40                      oldpath += pseudo_chroot_len;
  41              }
  (gdb) p oldname
  $1 = 0x6016835f1fb0 "/etc/group.3167327"
  (gdb) p oldpath
  $2 = 0x6016835f1fb0 "/etc/group.3167327"
  (gdb) p pseudo_chroot_len
  $3 = 91

The above code (on line 38) is supposed to check if oldpath is prefixed by pseudo_chroot, but instead it checks the opposite, and then the check for the slash on the next line is made outside oldpath if it is shorter than pseudo_chroot.

---
 ports/unix/guts/linkat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ports/unix/guts/linkat.c +1 -1
index e1712a9..60fbf63 100644
@@ -35,7 +35,7 @@
 	}
 #endif
 	oldpath = oldname;
-	if (pseudo_chroot_len && strncmp(oldpath, pseudo_chroot, pseudo_chroot_len) &&
+	if (pseudo_chroot_len && !strncmp(oldpath, pseudo_chroot, pseudo_chroot_len) &&
 		oldpath[pseudo_chroot_len] == '/') {
 		oldpath += pseudo_chroot_len;
 	}
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 3/3 test: Add test for linkat chroot path stripping

In reply to Mark Hatle

From: Mark Hatle <[email protected]>

A bug in linkat's chroot path stripping logic used strncmp() without negation, causing it to strip the chroot prefix when the path did NOT match. Also it accessed memory out of bounds when the path was shorter than pseudo_chroot_len. This was fixed in the previous commit.

Add a test that exercises this scenario by creating a chroot with a long directory path, then calling link() on short paths (/a -> /b) inside the chroot. The paths are placed at the end of a mapped memory page (via mmap) with the next page unmapped, so the out-of-bounds read at oldpath[pseudo_chroot_len] reliably causes a segfault rather than silently reading adjacent mapped memory.

AI-Generated: Test code written by Claude Opus 4.6.

---
 test/test-linkat-chroot.c  | 69 ++++++++++++++++++++++++++++++++++++++++++++++
 test/test-linkat-chroot.sh | 19 +++++++++++++
 2 files changed, 88 insertions(+)
 create mode 100644 test/test-linkat-chroot.c
 create mode 100755 test/test-linkat-chroot.sh
test/test-linkat-chroot.c new file +69 -0
new file mode 100644
index 0000000..e02989e
@@ -0,0 +1,69 @@
+/*
+ * Test that link/linkat inside a chroot does not segfault
+ * when the path is shorter than the chroot path.
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * The bug was that linkat's chroot path stripping used strncmp()
+ * without negation, causing an out-of-bounds read at
+ * oldpath[pseudo_chroot_len] when the path was shorter than the
+ * chroot path. To reliably trigger this, we place the path string
+ * at the end of a mapped page with the next page unmapped, so any
+ * out-of-bounds access will SIGSEGV.
+ */
+#define _GNU_SOURCE
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+
+/* Place str at the end of a page with the next page unmapped.
+ * Any read past the string will hit unmapped memory and SIGSEGV. */
+static char *guarded_string(const char *str) {
+    long pagesize = sysconf(_SC_PAGESIZE);
+    size_t len = strlen(str) + 1;
+    char *pages = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE,
+                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    if (pages == MAP_FAILED) {
+        perror("mmap");
+        return NULL;
+    }
+    if (munmap(pages + pagesize, pagesize) == -1) {
+        perror("munmap");
+        return NULL;
+    }
+    char *dest = pages + pagesize - len;
+    memcpy(dest, str, len);
+    return dest;
+}
+
+int main(int argc, char *argv[]) {
+    if (argc != 2) {
+        fprintf(stderr, "usage: %s <chrootdir>\n", argv[0]);
+        return 2;
+    }
+    if (chroot(argv[1]) == -1) {
+        perror("chroot");
+        return 1;
+    }
+    if (chdir("/") == -1) {
+        perror("chdir");
+        return 1;
+    }
+
+    char *oldpath = guarded_string("/a");
+    char *newpath = guarded_string("/b");
+    if (!oldpath || !newpath)
+        return 1;
+
+    /* link() calls linkat() internally. The short paths are placed
+     * at page boundaries so that the buggy out-of-bounds read at
+     * oldpath[pseudo_chroot_len] will SIGSEGV instead of silently
+     * reading adjacent memory. */
+    if (link(oldpath, newpath) == -1) {
+        perror("link");
+        return 1;
+    }
+    return 0;
+}
test/test-linkat-chroot.sh new file +19 -0
new file mode 100755
index 0000000..247a6d7
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Test that link/linkat inside a chroot does not segfault
+# when the path is shorter than the chroot path.
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+
+set -e
+
+# Create a chroot directory with a deeply nested path
+# so pseudo_chroot_len is longer than the filenames used inside
+CHROOTDIR=$(pwd)/linkat_chroot_test/deep/nested/path/to/make/it/long
+mkdir -p "$CHROOTDIR"
+touch "$CHROOTDIR/a"
+trap "rm -rf $(pwd)/linkat_chroot_test test-linkat-chroot" 0
+
+gcc -o test-linkat-chroot test/test-linkat-chroot.c
+
+./test-linkat-chroot "$CHROOTDIR"
Signature · 1 line
1.8.3.1
#4 lore raw
In reply to Mark Hatle
Quoted 9 lines · Mark Hatle

This includes the two pending patches to pseduo and a test case for Peter's issue so hopefully it doesn't happen again in the future.

These changes are currently applied to 'master-next'.  Assuming nobody finds issues with these, I'll merge to master and bump the version (1.9.6) on Monday.

(RP if you think the build needs it before Monday, go ahead!  I've tested everything and it looks safe to me.)

Thanks Mark.

6.0 rc1 is now built and ready for QA so I'm planning these for 6.0.1, therefore no rush from that perspective.

Cheers,

Richard