Linux Security Modules development
 help / color / mirror / Atom feed
From: Andrea Parri <parri.andrea@gmail.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	KP Singh <kpsingh@kernel.org>,
	Matt Bobrowski <matt@bobrowski.net>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Jan Kara <jack@suse.cz>, Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Chuck Lever <cel@kernel.org>, Jeff Layton <jlayton@kernel.org>,
	Anton Protopopov <a.s.protopopov@gmail.com>,
	linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org,
	linux-nfs@vger.kernel.org, linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [BUG] bpf: fs/xattr: bpf_set_dentry_xattr() hangs on path_link and on inode_getxattr from nfsd
Date: Sat,  5 Dec 2026 15:39:43 +0100	[thread overview]
Message-ID: <20261205143944.45754-1-parri.andrea@gmail.com> (raw)

Hi,

On 7.4-rc5 and next-20261204 (x86_64), a sleepable BPF LSM program that
calls bpf_set_dentry_xattr() hangs the calling task in the two cases
below.  Each case reproduced in 3 of 3 runs on each kernel, with the
program and commands at the end of this mail.

1. path_link, kfunc called on old_dentry

link("A", "A/x"), with A a directory, never returns.  Run as uid 65534
with no capabilities and with fs.protected_hardlinks=1, the task stays
in D state, also after SIGKILL, with security_path_link() and
bpf_set_dentry_xattr() on its stack.  Two tasks running link("A", "B/y")
and link("B", "A/x") in a loop both end up in D state with the same
stack.  lockdep reported nothing in these runs.

2. inode_getxattr, from nfsd

getxattr() on a file of a loopback NFSv4.2 mount never returns.  The
nfsd thread stays in D state, with nfsd_getxattr() and
bpf_set_dentry_xattr() on its stack, and lockdep reports recursive
locking.  The same getxattr() on the file's local path, run in the same
VM before the export, returns ENODATA.

bpf_set_dentry_xattr() was added by commit 56467292794b ("bpf: fs/xattr:
Add BPF kfuncs to set and remove xattrs").

The bug was found with the help of an LLM, which was also used to write
this report and the reproducer.

Reproducer
==========

repro.bpf.c:

  // SPDX-License-Identifier: GPL-2.0
  #include <linux/bpf.h>
  #include <bpf/bpf_helpers.h>
  #include <bpf/bpf_tracing.h>

  struct dentry {};
  extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
  				const struct bpf_dynptr *value_p, int flags) __ksym;

  char _license[] SEC("license") = "GPL";
  char value[1] = "x";

  static __always_inline void set_xattr(struct dentry *dentry)
  {
  	struct bpf_dynptr ptr;

  	bpf_dynptr_from_mem(value, sizeof(value), 0, &ptr);
  	bpf_set_dentry_xattr(dentry, "security.bpf.x", &ptr, 0);
  }

  SEC("lsm.s/path_link")
  int BPF_PROG(path_link, struct dentry *old_dentry)
  {
  	set_xattr(old_dentry);
  	return 0;
  }

  SEC("lsm.s/inode_getxattr")
  int BPF_PROG(inode_getxattr, struct dentry *dentry)
  {
  	set_xattr(dentry);
  	return 0;
  }

Build it as repro.bpf.o, from the kernel tree:

  make -C tools/lib/bpf
  clang -O2 -g --target=bpf -I tools/include/uapi -I tools/lib \
      -c repro.bpf.c -o repro.bpf.o

Both kernels were built with virtme-ng; "bpf" is in the resulting
CONFIG_LSM:

  vng --build --skip-modules \
      --configitem CONFIG_BPF_SYSCALL=y --configitem CONFIG_BPF_JIT=y \
      --configitem CONFIG_BPF_LSM=y --configitem CONFIG_SECURITY=y \
      --configitem CONFIG_SECURITY_PATH=y \
      --configitem CONFIG_DEBUG_INFO_DWARF5=y \
      --configitem CONFIG_DEBUG_INFO_BTF=y \
      --configitem CONFIG_FUNCTION_TRACER=y \
      --configitem CONFIG_DYNAMIC_FTRACE=y \
      --configitem CONFIG_PROVE_LOCKING=y --configitem CONFIG_STACKTRACE=y \
      --configitem CONFIG_TMPFS_XATTR=y \
      --configitem CONFIG_DETECT_HUNG_TASK=y \
      --configitem CONFIG_NFS_FS=y --configitem CONFIG_NFS_V4=y \
      --configitem CONFIG_NFS_V4_2=y --configitem CONFIG_NFSD=y \
      --configitem CONFIG_NFSD_V4=y

Each case ran in a fresh VM, started with:

  vng --run arch/x86/boot/bzImage --user root --cpus 2

The output below is from 7.4-rc5.

First, as root:

  mount -t bpf bpf /sys/fs/bpf
  bpftool prog loadall repro.bpf.o /sys/fs/bpf/repro autoattach
  mount -t tmpfs -o mode=1777 none /mnt

Case 1; the link() call, made as an unprivileged user, never returns:

  sysctl -w fs.protected_hardlinks=1
  setpriv --reuid=65534 --regid=65534 --clear-groups \
      python3 -c 'import os; os.mkdir("/mnt/A"); os.link("/mnt/A", "/mnt/A/x")'

The task is in D state, with /proc/<pid>/stack:

  bpf_set_dentry_xattr+0x38/0x70
  bpf_prog_5c3a9b6ead487187_path_link+0x55/0x5c
  bpf_trampoline_6442529122+0x65/0xf9
  security_path_link+0x39/0x60
  filename_linkat+0x232/0x2c0
  __x64_sys_linkat+0x55/0x80

The ABBA variant left two tasks with the same stack in each run:

  mkdir /mnt/A /mnt/B
  python3 -c '
  import os
  if os.fork(): a, b = "/mnt/A", "/mnt/B/y"
  else:         a, b = "/mnt/B", "/mnt/A/x"
  while True:
      try: os.link(a, b)
      except OSError: pass'

Case 2 first reads the file locally; this getxattr() returns ENODATA:

  echo x > /mnt/f
  python3 -c 'import os; os.getxattr("/mnt/f", "user.x")'

It then exports /mnt over NFSv4.2 to 127.0.0.1 and mounts it on /srv.
The writes to /proc/net/rpc stand in for rpc.mountd:

  mount -t nfsd nfsd /proc/fs/nfsd
  echo "-3 +4 +4.1 +4.2" > /proc/fs/nfsd/versions
  echo "tcp 2049" > /proc/fs/nfsd/portlist
  echo 1 > /proc/fs/nfsd/threads
  N=2147483647
  echo "nfsd 127.0.0.1 $N d" > /proc/net/rpc/auth.unix.ip/channel
  printf 'd 1 \\x00000000 %s /mnt\n' $N > /proc/net/rpc/nfsd.fh/channel
  echo "d /mnt $N 9218 65534 65534 0" > /proc/net/rpc/nfsd.export/channel
  mount -t nfs4 -o vers=4.2,addr=127.0.0.1 127.0.0.1:/ /srv
  python3 -c 'import os; os.getxattr("/srv/f", "user.x")'

The getxattr() never returns, and the nfsd thread is in D state:

  bpf_set_dentry_xattr+0x38/0x70
  bpf_prog_5c3a9b6ead487187_inode_getxattr+0x55/0x5c
  bpf_trampoline_6442529050+0x61/0xf9
  security_inode_getxattr+0x34/0x60
  vfs_getxattr+0x7f/0x150
  nfsd_getxattr+0x7b/0x220
  nfsd4_proc_compound+0x2f3/0x800

  WARNING: possible recursive locking detected
  nfsd/98 is trying to acquire lock:
  (&sb->s_type->i_mutex_key#9){++++}-{4:4}, at: bpf_set_dentry_xattr+0x38/0x70
  but task is already holding lock:
  (&sb->s_type->i_mutex_key#9){++++}-{4:4}, at: nfsd_getxattr+0x64/0x220

Thanks,
Andrea

                 reply	other threads:[~2026-12-05 14:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205143944.45754-1-parri.andrea@gmail.com \
    --to=parri.andrea@gmail.com \
    --cc=a.s.protopopov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=cel@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=matt@bobrowski.net \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox