From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D144A175A84 for ; Wed, 8 Jul 2026 00:10:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783469406; cv=none; b=tUWvIeHbWI0I5z7yehUBrMK284G8fabYpY9wKlVxVcC2/ajd05ZhFfJXDQsUM0TPIZUju01zoXtqkNc7yxu1DzYy1wdotDVERssOk2z4vphTNOvuDXrJmzc+iPu9gCXoQplQ0cvDdaLgiUd3s5AwkEVUtgPUTqR0ge6x94fl6k0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783469406; c=relaxed/simple; bh=5paAE7FcwR2EAGMv6ATIvCwCHK8moB/MJ/jdRXtXdK8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B6O/darolTCDr1hNPvJeSHlzuM+wcvv22gl6I3+dS9lX1/32hx4B9owsRP/JMA7MFJeakSGPmQ9lMtOEiNcbu89p4yTbS/gOciyeYUmC3cwWXOyqakx/wbaifAj4ju8ZcjdHT36TndRvvwbEuPGN9w2vjIvA39P3Oa6sPqp2rOo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UuAn6QDg; arc=none smtp.client-ip=209.85.222.173 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.b="UuAn6QDg" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-92e622cc874so2626885a.0 for ; Tue, 07 Jul 2026 17:10:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783469403; x=1784074203; 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=H5d7fbQwym2HIgVRnDv1JutJFyLmAUJ8aFrpYmfGtZs=; b=UuAn6QDg7TzhKkdh0r7yGQXXc3Uq+WJb9ODMXtF6L17F4PL1UWjCZl3Uw25JjIhGht QBvIw6FC6dz0zTcQ+pgOleJY5gjbuMrKzjJR2kLYFG/NVsz/9KbOS6vBCtHyfaGw0RH+ GezOXCtCDkWLZ2qaeDO9KKPQkvRvxvRJJxVnmCxsB40eJMmwnpdBlBN+CQH8RweOYAIy hw4rUrrzVFqq+wSAQfDfNRIwq0FF3FjyXonh6AimYjWWtzVsJmer8erzylurjvN6ZHLM KAQV8Cj6C3/xXUwOe6FNc3UATVxC90S0r0BXZ+dNK1YuSg9xq2x7Y6NfV/HWvxdnkCCu 2Nkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783469403; x=1784074203; 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=H5d7fbQwym2HIgVRnDv1JutJFyLmAUJ8aFrpYmfGtZs=; b=Bq0Uuka14QdQ/hzDTh+PHKh6/MGyk7gW4a5dx3OSwTRc2LGXKhrxXBpIRcKi50SDpn OSQFhnvmpeEBlnWWKjoqPUbkndmgTx0AvJ8S/YcrzGn1KlewMH3XB7WVfB+mLQD8ElWO KAGtVwI9FCVXaJo9wf514fADqRhFkenP3Vri69MpUb4yj94Cy5GOaRPQyX+HFW1YIwQs suRouJ5SqXe9GcXn7DXeVa+63v37gQU9l40vG0EZt2sJ8DWGX30lTsZlkwYYwHsE4LIv /zDWb9Y0A9mVV5pFNQ++aSo+r8QDxEqCAaKdmeEsAdJLY5fWWk+yWP20Z/yrFqDVkZOA Y1QQ== X-Forwarded-Encrypted: i=1; AHgh+RpTlUReJ10eGzRC3NWxn65ZxQpJyOTgMuDpaNRi1tJFDi50NloeJYuqvSjKiG/hoMMM4snRsEQZ4yb+6zFNoqV/aVF910s=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5yZqH1c42vy+MOgnaaVkomPWT0d+0KpCuAk6GVT07EgxOk/YL dzrboMbpl9S7EGze7jZa3zwyfbCXqXuSdFubAnZdFWsoxAnTC8aBlJYY X-Gm-Gg: AfdE7cknINoFWUe0yA4M3q2iVfWKQ3QIkdW62esqAoc5bMLm2qYpDWnUFEyvMsjZU1M oiLOwY6nuPXpBzCRKmgUE1YakYB0chWoE/qI2A1hc/VbWxtIos+xoVxvK0KZB4enTldYo23u4VU /EqLy8EAw1I4RCG50rJTDU3XtFvkxsazU+rP40Yz0Xr1Id2a8HgPVYRKYKYgkacQWXLqx/NpZ3Q +y1XTUA1cVs8QvmDmro7Xio2nQBw+W2McygQ30d9L4iXlWasU09ePTYCkGSd3POjGqGwRZxcmDA YbdjFLi/BDowLPZH4C3qTXmeGamcug6R2KBXHJjYzSff/NiFfIij08GRiVQXBNfPctWWNwN6SV2 VLfsWI418Nxo9aqbulqptNVTAALKKy0hA5Yqy9wc6taaKLAp3/ROlJ7GFp8L9I5lbKHSxFzDLMw eCIAmzLXEZev4Uvy/O0mdDQOM+Gx3QVRWXkmzGyzmoYr5I7f8WyhX4CeOcH6StBw4wWpI0RQt9T LnxDfNyHwBCW0A= X-Received: by 2002:a05:620a:8804:b0:92e:59b0:df08 with SMTP id af79cd13be357-92ecf5bf0d2mr8803585a.3.1783469402697; Tue, 07 Jul 2026 17:10:02 -0700 (PDT) Received: from battery.lan (pool-138-88-31-60.washdc.fios.verizon.net. [138.88.31.60]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92e93ea5ed3sm1191281885a.29.2026.07.07.17.10.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2026 17:10:02 -0700 (PDT) From: David Windsor To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Jiri Olsa , Kumar Kartikeya Dwivedi , Emil Tsalapatis , Matt Bobrowski , Paul Moore , James Morris , "Serge E . Hallyn" , Casey Schaufler , Stephen Smalley , Ondrej Mosnacek , Mimi Zohar , Roberto Sassu , Dmitry Kasatkin , Eric Snowberg , Alexander Viro , Christian Brauner , Jan Kara , Shuah Khan Cc: bpf@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org, selinux@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, David Windsor Subject: [PATCH v5 bpf-next 2/3] bpf: add bpf_init_inode_xattr kfunc for atomic inode labeling Date: Tue, 7 Jul 2026 20:09:55 -0400 Message-ID: <20260708000956.46138-3-dwindsor@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260708000956.46138-1-dwindsor@gmail.com> References: <20260708000956.46138-1-dwindsor@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add bpf_init_inode_xattr() kfunc for BPF LSM programs to atomically set xattrs via the inode_init_security hook using security_lsmxattr_add(). The hook now passes its xattr state as a single struct lsm_xattrs object, which the kfunc takes directly. This kfunc is only callable from inode_init_security; the verifier rejects attempts to call it elsewhere. A previous attempt [1] required a kmalloc string output protocol for the xattr name. Since commit 6bcdfd2cac55 ("security: Allow all LSMs to provide xattrs for inode_init_security hook") [2], the xattr name is no longer allocated; it is a static constant. Link: https://kernsec.org/pipermail/linux-security-module-archive/2022-October/034878.html [1] Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6bcdfd2cac55 [2] Suggested-by: Song Liu Signed-off-by: David Windsor --- fs/bpf_fs_kfuncs.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 768aca2dc0f0..ad0025f2264a 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -11,7 +11,9 @@ #include #include #include +#include #include +#include __bpf_kfunc_start_defs(); @@ -374,6 +376,39 @@ __bpf_kfunc struct inode *bpf_real_inode(struct dentry *dentry) return d_real_inode(dentry); } +/** + * bpf_init_inode_xattr - set an xattr on a new inode from inode_init_security + * @xattrs: inode_init_security xattr state from the hook context + * @name__str: xattr name (e.g., "bpf.file_label") + * @value_p: dynptr containing the xattr value + * + * Only callable from lsm/inode_init_security programs. + * + * Return: 0 on success, negative error on failure. + */ +__bpf_kfunc int bpf_init_inode_xattr(struct lsm_xattrs *xattrs, + const char *name__str, + const struct bpf_dynptr *value_p) +{ + struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; + const void *value; + u32 value_len; + + if (!name__str) + return -EINVAL; + if (strncmp(name__str, XATTR_BPF_LSM_SUFFIX, + sizeof(XATTR_BPF_LSM_SUFFIX) - 1)) + return -EPERM; + + value_len = __bpf_dynptr_size(value_ptr); + value = __bpf_dynptr_data(value_ptr, value_len); + if (!value) + return -EINVAL; + + return security_lsmxattr_add(xattrs, LSM_ID_BPF, name__str, value, + value_len); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_fs_kfunc_set_ids) @@ -385,6 +420,7 @@ BTF_ID_FLAGS(func, bpf_get_file_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_inode, KF_SLEEPABLE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_init_inode_xattr) BTF_KFUNCS_END(bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) -- 2.53.0