Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796398988; b=JZKMBON8KCwyvcJ75nrZ3OKLU5lWkLJffiBCoiBMu7Z2iGbQWlrWtJuYpqDLXcGXm0gO xn2mzf1W/Lj6uBuduR85cSHczZoTQPKNjqQTj3Co1ibSt5wTRsILX0LHJ4iqDsY2hK5jQ 1uaKdHzirwpW97BTDntMgJa6oZcysLYzMd1pHGnl6aVYB7A+44xgJqTSW53s3nMqVQb9X pQK7OHSJE8O/IIn+kZhpwCZRYFtgSMiG7ZH3pNQuDBFZPWlLRpScGXwJkQ00RrXWWTJ16 AgXqnFBDzfNENHoi/ERcMKGejEy86g2R2vDu+VDPEP82sEJTNfGIGML+7T170mrWfMg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796398988; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=s1kytqHyW3NAtOi+ladnDJ0QouNCWDGMny3X8BxdHEg=; b=yxHjW9+jhqJM0ff77PNtZPfB/nVKL6zT6Nu4Va7MQ2NL9oam7/Soh1DYYpj7nz0K+8yk 0j5v949UmRzfqVir+TBU10VgM/unJMKrbkhGsYWZ6eTWSRJ2H+8NeEAZz95rKwKOoPoz6 5NnEVyY5x8ZHmVeZE4tq9NjK3AP3Z/dkIw0V8+k1E0XRWhwc5TTtj6lC4qUBuaNye8PBH xAEdv2zCSSvIx3oNL3ndRnXKa00ehNREIxdygCOiP9IoNq2aRHKHAAdznZUUKHLNvmHzn VUKT7cIYSwjkI8QOEpwdMpLTMxZgTwtFWkk+SNULmeXZWsHGBWRv18Iyg4Z59TnA4OA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yJkshtzA; arc=none smtp.remote-ip=209.85.128.42 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75CD160A260A for ; Fri, 4 Dec 2026 15:43:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 75CD160A260A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yJkshtzA Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4bb6886d30aso10581075e9.3 for ; Fri, 04 Dec 2026 07:43:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796398986; x=1797003786; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=s1kytqHyW3NAtOi+ladnDJ0QouNCWDGMny3X8BxdHEg=; b=yJkshtzA8rVHwlCQquoTw5gE/H5ExTHdgUM6SntCoytvFFgF6Ot8eUHk7jhAPpdrAd G0m43GBWSthsS3Ih673pgWq9s61OojHCSy2mJCxb//RAHJ7O/r4JH62HO5/4y8JQWfHy CR+MfQuB9bgMbxZJorhJXRdBMI/arUzValruMwDXPzxeLP6uah8cAdxpYKnFP8SrhNV4 jZ73tZ567C23Y7qfDxqhS5Fe492u4sknLIDWr3uj7T8aHJ8sm11PEH4f4mEtcot9P3Lb gjUedG36FVn2GiGN/98vFoHXI47Qwllmcr6VWl9RlTRptKYcGq5Aj4p/dqKu6gAixhte HSyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796398986; x=1797003786; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=s1kytqHyW3NAtOi+ladnDJ0QouNCWDGMny3X8BxdHEg=; b=NN6vAMggwnCjApCj2W424fjoMlbEwFG9sOarcXqj1Ew3MvI7t2b4qshHDCWzH1tcXl UUAOSCUc0uOtQMxn2jiz/ERAdvY5VZS1sO7r6UYf3jFCEgZ3leuNhJNYmt6xi2JS2Rov cscSSy789Ylfb9kc81Bdw2Vjb2NKkhmevN75Goe440HmDNGiP6Cvdg6i0+ld5Qe1tLhJ u5FPmqNR9ec2muEUIR3R/rbQQeH9JEKNztctv2RQOrRiGTAwIHf+2oqyNI40+a8PEn/i HPfHC02Q3QcMCMfQKJ9MYRQB/jou1owT5C/N2/iUcCY02wK5IWPOvK80nyoj3uEFR1HH CU1Q== X-Forwarded-Encrypted: i=1; AL+hErFpKBrR3fRAE/c1JjGYAJWgpQFlHxexy8nmjuA4ZjJZvVTGtFRt+YYqop8kfi4xD1ZKvp4=@vger.kernel.org X-Gm-Message-State: APuU4Z8K5AH7ELKrca6vGQUYt9eoRvsYMyPbifF7T6L7EfSdvyVPryak Ucsfq+Aywn4PD+17gVktI04LaxlIL8186oUfMTnBUEsKtV+pvLh2Wclu X-Gm-Gg: AZFdTZcd4mowrGjvIQGsfa0SvPDqGJ8sGW5a38yk6QUFkuKES6zKJGlQ3Sd73wu6XOS bY5PuawkbOyMD013esnP2VhKEh1u9b6A5HXTfdNqMta4WPvCEjcWltUAMJkheId0y1ZQJ9khTfI IrhzQUrc6wYVr0natEqgQmyX3KFg3YjRb6ZNjqqF3TmqBeymhdncHYhfAtqA792VLkGmfwxyM57 S/dYpueeusdg+ntFj2t1hO72Z1LR5T+4HT/xC8x6uCPXpg2KquZbVV0pTgY3fZySZMQKVVU8/Fn UNJJcH1EAOWJY4xFxRYeXsZ1dHkZh6Lj6SJ/SeuxEn4hOgeiLq+Ue83nyrYRN7KzkQ7C6/026Vz oQSy2mUp/M4+7rMggwJwv0i012aoZOaK/HYcmZCCyyxKQVesgtN0qWqYk3IkjEF9WCyWYGI6BwB 4VkNT0R4BxuJjK5bRgLTulGYLC7m8L44AjVF/TtdCZIPYNpBotWfmYIamDf7ef0vKkZS+Vl6Owa TqjXhbt14VP147fwn8e8DzCP2YP+dL5hoQn94bfo3EVRO2w5XZVpai/kAZ42f7f5DzAGbJVuIqq hfQ6Ki5s3dVSObRbfwFNgvg2dYu7bDVEqCa8LNJcuaZziFw05EOkalcetTcw3T6u46i9 X-Received: by 2002:a05:600c:1f92:b0:4a2:c2b:a91b with SMTP id 5b1f17b1804b1-4bbeb850f9cmr48567245e9.10.1796398985966; Fri, 04 Dec 2026 07:43:05 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbea25ce51sm63147535e9.5.2026.12.04.07.43.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 07:43:05 -0800 (PST) From: Anton Protopopov To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , KP Singh , Matt Bobrowski , Alexander Viro , Christian Brauner , Jan Kara , Mimi Zohar , Roberto Sassu , bpf@vger.kernel.org, linux-integrity@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 5/6] bpf: Add a bpf_get_inode_xattr kfunc Date: Fri, 4 Dec 2026 15:54:35 +0000 Message-ID: <20261204155438.1413411-6-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204155438.1413411-1-a.s.protopopov@gmail.com> References: <20261204155438.1413411-1-a.s.protopopov@gmail.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The inode_create, inode_mkdir, inode_mknod, inode_symlink, inode_link and inode_rename hooks get the parent directory as an inode and the new entry as a dentry. To check a label of the directory, a program needs to read its xattr, but bpf_get_dentry_xattr() takes a dentry. The only dentry of the directory at hand is dentry->d_parent, and the verifier doesn't trust it. Making d_parent trusted would be wrong. It would be trusted on every hook, and many hooks don't hold the lock of the parent directory. For example, inode_getxattr and inode_readlink hold no lock at all. A concurrent rename can then move the dentry to another directory, and the old parent can be freed while a sleepable program still uses it. Add the bpf_get_inode_xattr() kfunc which reads an xattr of an inode through one of its dentries. Xattrs belong to the inode, but some filesystems like cifs build a path from the dentry. So take a hashed dentry with d_find_alias() if there is one. An unlinked file which is still open has none, so take any with d_find_any_alias() then, as inode_doinit_with_dentry() in SELinux does. The kfunc doesn't change xattrs, so the setters still get only the dentries passed to the hook, and d_inode_locked_hooks stays correct for them. Signed-off-by: Anton Protopopov --- fs/bpf_fs_kfuncs.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 6021e9f60ad2..4e2b371fd0b5 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -169,6 +169,38 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, return bpf_get_dentry_xattr(dentry, name__str, value_p); } +/** + * bpf_get_inode_xattr - get xattr of an inode + * @inode: inode to get xattr from + * @name__str: name of the xattr + * @value_p: output buffer of the xattr value + * + * Get xattr *name__str* of *inode* and store the output in *value_ptr*. + * The xattr is read through a hashed dentry of *inode* if it has one and + * through any dentry otherwise. + * + * For security reasons, only *name__str* with prefixes "user." or + * "security.bpf." are allowed. + * + * Return: length of the xattr value on success, -ENOENT if *inode* has + * no dentry, another negative value on error. + */ +__bpf_kfunc int bpf_get_inode_xattr(struct inode *inode, const char *name__str, + struct bpf_dynptr *value_p) +{ + struct dentry *dentry; + int ret; + + dentry = d_find_alias(inode); + if (!dentry) + dentry = d_find_any_alias(inode); + if (!dentry) + return -ENOENT; + ret = bpf_get_dentry_xattr(dentry, name__str, value_p); + dput(dentry); + return ret; +} + __bpf_kfunc_end_defs(); static int bpf_xattr_write_permission(const char *name, struct inode *inode) @@ -506,6 +538,7 @@ BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) BTF_ID_FLAGS(func, bpf_path_d_path) BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_get_inode_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_real_data_inode, KF_SLEEPABLE | KF_RET_NULL) -- 2.43.0