From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795435436; b=IJOMUjBOGqpFHfuQNu8TyvoZexjMjbL1YguspyGXJtD+vshg0SOUYeAayAQCRJLCHtiB BpevDjyGbDbqPPt/u9Nr2iTbGRISN/gb1oRHDlibcILlxwQZeOBRU55fGBncNgo2JtUMP tQJsnpReR/Dp30RqfGoobXFXYIUxbRdhiqXnS5ak7hSWGywY53SjALyCgkXycwfpj48Wq woyZAaVeFR+a3UR6DbdtF/Mye7urpwHam3djAhPffbnKTtJH93yiItYIjQ1WEBsqqp1Vv 3NMMIL06VlA2NiS1klDWI64TwNe1d6PWZMcUnl9Fk5Ks+USo+rBBgtgKKoO0yG8FFCw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795435436; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=os2YYoEwaN9EE55QVw/0ZexEHa3Izs1qGFG8A6wk42w=; b=Ap6Xou7Wz5lkXMrfLuC69+1MDSUAJmVtTtga6VGc1v8BrtQMIqU7YxqD3AY/G63wXYA5 dLGJcl0TB9j99aJu0Q/SvtAyJg/kz6p25F4Z/UVTLVWP60Wuu7lol5/jR59WRV9UCzjOG SGdGBGHyCR7Qps5GojEqP2vYQDTeDrCLGsPeSXE76zB1FD+3gj1osAwxwIc1DBURNecRz jGtTSZlgtiJa//IazFlX00L1pUGfCJkqrnvR60hFInDv/VLALxoUE8VicKVrZt3pjQCI8 zMoTx3n6w+5xSn4qZ77TLw5TpJCMC8kT4HsyUYCl2krxtncg9h28i+d61ZNc7jKl7cg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bk131Rxl; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 034F9606A321; Mon, 23 Nov 2026 12:03:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bk131Rxl Received: by smtp.kernel.org (Postfix) with ESMTPSA id 200111F000C0; Mon, 23 Nov 2026 12:03:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795435435; bh=os2YYoEwaN9EE55QVw/0ZexEHa3Izs1qGFG8A6wk42w=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=bk131Rxlpj3POpKOb+DjjJ+q3NcgXQQtTUX0T+UCgf/zH/mbkic7H7lYzoUsnjlF5 XL0Cg6Kigoyn13fn02GhFVnNESccz84p5lNrMIAYvmDbgXuF/uGEeDdmLPdjdmx9RU bs88zb7OuwQK6vfpg9GV05i52Toi5s1/tmHFvVRF11lO+AGcSnMu0rgidpBTmPUS9Z zquBrDHHy217zx/8v/qb8vQMvlTnD4gLh9Hqgv52RcajDBC0EfszV0EJirZHrnWhfE fGiWWmfOhO8xgfuoL9KxCkrQVZhPW12Mf9UDJg0ZwMsYxZrHywqZ+z8fixBoiXOdhd 4t0QOX/fxVVgQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH v4 4/6] security: kap: add the securityfs control plane Reply-To: sashiko-reviews@lists.linux.dev To: "HARIHARA SUDHAN. R" , bpf@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org Cc: "Liam R. Howlett" , "Serge E. Hallyn" , "Andrew Morton" , "Ben Segall" , "David Hildenbrand" , "Dietmar Eggemann" , "Ingo Molnar" , "James Morris" , "Jonathan Corbet" , "Juri Lelli" , "K Prateek Nayak" , "Kees Cook" , "Ketan Bhardwaj" , "Lorenzo Stoakes" , "Mel Gorman" , "Michal Hocko" , "Mike Rapoport" , "Paul Moore" , "Peter Zijlstra" , "Randy Dunlap" , "Shuah Khan" , "Steven Rostedt" , "Suren Baghdasaryan" , "Valentin Schneider" , "Vincent Guittot" , "Vlastimil Babka" In-Reply-To: <20261123114308.8103-5-harisudhan@armoriq.io> References: <20261123114308.8103-1-harisudhan@armoriq.io> <20261123114308.8103-5-harisudhan@armoriq.io> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 23 Nov 2026 12:03:53 +0000 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [Medium] security: kap: dentry reference leak on child files in kapfs_mkdir() - [Medium] security: kap: unattached kapfs_mnt resolves as root-relative in d_path() - [Medium] security: kap: data race on kap_task_state in kap_bind_pid_locked() - [Medium] security: kap: race between kap_bind_pid_locked() and copy_process() - [Medium] security: kap: kap_authority_prepare() bounds predicates by next kind not '}' - [Medium] security: kap: unbounded igroup->pids growth in kapfs_write() - [Low] security: kap: kapfs_write() silently succeeds on newline-only writes - [Low] security: kap: order-5 allocation and locked strlen() in kapfs_write() -- commit 9e7e1166b9d68fb071806f2526682914d4f5f018 Author: HARIHARA SUDHAN. R security: kap: add the securityfs control plane This commit adds the securityfs control plane under /sys/kernel/security/kap and mounts an internal kapfs filesystem for creating and configuring igroup= s. > diff --git a/security/kap/kapfs.c b/security/kap/kapfs.c > new file mode 100644 > index 0000000000000..c9f03c2d8c0ac > --- /dev/null > +++ b/security/kap/kapfs.c [ ... ] > @@ -0,0 +1,530 @@ > +static int kapfs_show(struct seq_file *m, void *v) > +{ > + struct kap_file_ctx *ctx =3D m->private; > + struct kap_igroup *igroup =3D ctx->igroup; > + struct kap_bound_pid *entry; > + > + spin_lock(&kap_lock); > + switch (ctx->kind) { > + case KAP_FILE_PROCS: > + list_for_each_entry(entry, &igroup->pids, node) > + seq_printf(m, "%d\n", entry->pid); > + break; [Severity: Medium] Does igroup->pids grow without bound over the lifetime of an igroup? Every new PID written to kap.procs allocates a struct kap_bound_pid in kapfs_write() and appends it to igroup->pids in kap_bind_pid_locked(), but entries are never removed or freed when a task exits in kap_task_free() or when a task is rebound to another igroup. As processes are spawned, bound and exit, igroup->pids leaks memory, reports dead PIDs on kap.procs reads, and linearly walks an ever-growing list under the global kap_lock spinlock in both kap_pid_in_igroup_locked() and kapfs_show(). [ ... ] > +static ssize_t kapfs_write(struct file *file, const char __user *buf, si= ze_t len, loff_t *ppos) > +{ [ ... ] > + if (len =3D=3D 0) > + return 0; > + if (len > (ctx->kind =3D=3D KAP_FILE_INSTALL ? KAP_MAX_INSTALL_BYTES > + : KAP_MAX_PREDICATE_BYTES)) > + return -EINVAL; > + if (ctx->kind =3D=3D KAP_FILE_EVENTS || ctx->kind =3D=3D KAP_FILE_IGFD) > + return -EINVAL; > + > + tmp =3D memdup_user_nul(buf, len); > + if (IS_ERR(tmp)) > + return PTR_ERR(tmp); > + kap_trim_newline(tmp); > + if (!tmp[0]) { > + kfree(tmp); > + return len; > + } [Severity: Low] Should kapfs_write() silently succeed when the buffer is empty after stripping trailing newlines? Writing a newline-only string (such as echo "" > kap.procs or kap.mode) returns len without validating the input or updating any state, and also makes it impossible to clear kap.lineage_root via a single-file write even though kap.install accepts an empty lineage_root=3D value. > + > + /* Parse, allocate and compile first: kap_lock is a spinlock. */ > + switch (ctx->kind) { [ ... ] > + case KAP_FILE_INSTALL: > + rc =3D kap_install_parse(tmp, &req); > + if (!rc) > + rc =3D kap_authority_prepare(req.authority, &compiled); > + break; > + case KAP_FILE_AUTHORITY: > + rc =3D kap_authority_prepare(tmp, &compiled); > + break; [Severity: Medium] Can kap_authority_prepare() compile patterns from the next predicate into an earlier predicate when JSON keys precede "kind"? Both kap_validate_authority_payload() and kap_compile_kind_array() walk backward from "kind" to '{' to find predicate_start, but bound predicate_end by the next "kind" rather than the current object's closing '}': security/kap/igroup.c:kap_compile_kind_array() { ... while ((predicate =3D kap_next_predicate(predicate, end, kind_name))) { const char *next =3D strstr(predicate + 1, "\"kind\":"); const char *predicate_start =3D predicate; const char *predicate_end =3D next && next < end ? next : end; int rc; while (predicate_start > auth && *predicate_start !=3D '{') predicate_start--; rc =3D kap_compile_array(compiled, set, predicate_start, predicate_end, array_name); ... } If one predicate omits its "allow" or "deny" array and the following predicate places "allow" or "deny" before "kind", those arrays fall inside [predicate_start, predicate_end) of the earlier predicate. kap_validate_authority_payload() then accepts the malformed predicate and kap_compile_kind_array() compiles the next predicate's patterns under the wrong hook kind. [ ... ] > + spin_lock(&kap_lock); > + switch (ctx->kind) { > + case KAP_FILE_PROCS: > + rc =3D kap_bind_pid_locked(igroup, pid, &entry); > + break; [Severity: Medium] Can kap_ptrace_target() observe torn or partially initialized task state while kap_bind_pid_locked() updates a task? kap_bind_pid_locked() sets state->active =3D true before writing state->epoch and state->igroup_name, while kap_ptrace_target() reads those fields without holding kap_lock: CPU0 CPU1 kapfs_write() spin_lock(&kap_lock) kap_bind_pid_locked() state->active =3D true kap_ptrace_access_check() kap_ptrace_target() if (child_state->active) strcmp(..., child_state->igroup_name) <-- reads empty or torn name state->epoch =3D igroup->epoch strscpy(state->igroup_name, ...) A concurrent ptrace check can therefore misclassify same-igroup versus cross-igroup ptrace operations and log a corrupted target string in kap.events. [Severity: Medium] Can a thread or child being created in copy_process() miss the binding when kap_bind_pid_locked() runs concurrently? kap_task_alloc() copies the parent's kap_task_state during security_task_alloc(), long before copy_process() links the new task onto signal->thread_head under siglock: CPU0 CPU1 copy_process() security_task_alloc() kap_task_alloc() spin_lock(&kap_lock) /* old->active is false */ spin_unlock(&kap_lock) kapfs_write() spin_lock(&kap_lock) kap_bind_pid_locked() for_each_thread(task, t) state->active =3D true spin_unlock(&kap_lock) write_lock_irq(&tasklist_lock) list_add_tail_rcu(&p->thread_node, &p->signal->thread_head) <-- new task runs with active =3D=3D false Because p is not yet on signal->thread_head when for_each_thread() walks the process, p escapes the igroup binding even though writing to kap.procs succeeded. [ ... ] > + case KAP_FILE_AUTHORITY: { > + struct kap_compiled_authority *old =3D igroup->compiled; > + > + strscpy(igroup->authority, tmp, sizeof(igroup->authority)); > + igroup->authority_len =3D strlen(igroup->authority); > + igroup->compiled =3D compiled; > + compiled =3D old; > + break; > + } [Severity: Low] Could kapfs_write() avoid the order-5 physically contiguous allocation in memdup_user_nul() and the extra strlen() pass under kap_lock? For KAP_FILE_INSTALL and KAP_FILE_AUTHORITY, len can be up to 66,560 bytes, so memdup_user_nul() requests up to 66,561 physically contiguous bytes (order 5 on 4 KB page kernels, above PAGE_ALLOC_COSTLY_ORDER), which can stall in direct compaction or fail under fragmentation; using vmemdup_user() or kvmalloc() would match kvzalloc_obj() in kap_authority_prepare(). In addition, using the length returned by strscpy() here (and in kap_install_locked()) avoids rescanning up to 64 KB with strlen() while holding the global kap_lock spinlock. [ ... ] > +static void kapfs_instantiate(struct dentry *dentry, struct inode *inode) > +{ > + d_make_persistent(dentry, inode); > +} [ ... ] > +/* Returns NULL on success: the passed dentry is the one instantiated. */ > +static struct dentry *kapfs_mkdir(struct mnt_idmap *idmap, struct inode = *dir, > + struct dentry *dentry, umode_t mode) > +{ [ ... ] > + for (i =3D 0; i < KAP_FILE_COUNT; i++) { > + files[i] =3D d_alloc_name(dentry, kap_file_names[i]); > + inodes[i] =3D kapfs_make_inode(dir->i_sb, S_IFREG | kap_file_modes[i]); > + if (!files[i] || !inodes[i]) { > + rc =3D -ENOMEM; > + goto err; > + } > + igroup->files[i].kind =3D i; > + igroup->files[i].igroup =3D igroup; > + inodes[i]->i_fop =3D &kapfs_fops; > + inodes[i]->i_private =3D &igroup->files[i]; > + } [ ... ] > + /* Lookups in the new directory wait until its files are all there. */ > + inode_lock(inode); > + kapfs_instantiate(dentry, inode); > + igroup->dentry =3D dentry; > + for (i =3D 0; i < KAP_FILE_COUNT; i++) > + kapfs_instantiate(files[i], inodes[i]); > + inode_unlock(inode); > + return NULL; [Severity: Medium] Does kapfs_mkdir() leak a dentry reference on every child file in files[]? d_alloc_name() returns each files[i] with a reference count of 1, and kapfs_instantiate() -> d_make_persistent() takes an additional reference via dget_dlock(). Unlike the directory dentry passed into kapfs_mkdir(), whose creation reference is dropped by the VFS caller, files[i] is never passed to dput() after kapfs_instantiate(), leaving each child file with a reference count of 2 instead of 1. [ ... ] > +static const char *kap_igroups_get_link(struct dentry *dentry, > + struct inode *inode, > + struct delayed_call *done) > +{ > + struct path path; > + int rc; > + > + if (!dentry) > + return ERR_PTR(-ECHILD); > + path.mnt =3D mntget(kapfs_mnt); > + path.dentry =3D dget(kapfs_mnt->mnt_root); > + rc =3D nd_jump_link(&path); > + return ERR_PTR(rc); > +} [ ... ] > +int __init kap_securityfs_init(void) > +{ > + struct dentry *dent; > + int rc; > + > + if (!READ_ONCE(kap_lsm_active)) > + return 0; > + > + kapfs_mnt =3D kern_mount(&kapfs_type); [Severity: Medium] When a task crosses the igroups symlink via nd_jump_link() onto the internal kern_mount() vfsmount, won't d_path() resolve files under /sys/kernel/security/kap/igroups as //? Because kapfs_mnt is an unattached internal mount with no parent mountpoint, d_path() in kap_file_target(), kap_lsm_file_path_alloc() and kap_lsm_check_dir_entry() stops at kapfs_mnt->mnt_root and formats paths without the /sys/kernel/security/kap/igroups prefix. File policies written for /sys or /sys/kernel/security/kap will therefore fail to match accesses under igroups/ (and an igroup named "proc" will be misclassified as procfs by kap_path_is_procfs()), while kap.events logs root-relative paths. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261123114308.8103= -1-harisudhan@armoriq.io?part=3D4