mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: John Johansen <john.johansen@canonical.com>,
	 Georgia Garcia <georgia.garcia@canonical.com>,
	 Paul Moore <paul@paul-moore.com>,
	James Morris <jmorris@namei.org>,
	 "Serge E. Hallyn" <serge@hallyn.com>
Cc: apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org,
	 linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	 Arun Iyengar <aiyengar1990@gmail.com>
Subject: [PATCH] apparmor: don't sleep in unix peer checks for path sockets
Date: Sat, 21 Nov 2026 21:29:15 +0000	[thread overview]
Message-ID: <20261121-apparmor-unix-peer-v1-1-99dd1cdb1e27@gmail.com> (raw)

From: Arun Iyengar <aiyengar1990@gmail.com>

security_unix_may_send() and security_unix_stream_connect() run under
unix_state_lock(), but the path check of a path-bound peer calls
aa_path_perm(), which gets its buffer with aa_get_buffer(false):

  BUG: sleeping function called from invalid context at security/apparmor/lsm.c:2301
   aa_get_buffer+0x348/0x3a0
   aa_path_perm+0xdc/0x268
   unix_fs_perm+0x1d8/0x1e8
   unix_peer_perm+0x1d8/0x588
   aa_unix_peer_perm+0xe8/0x150
   apparmor_unix_may_send+0x108/0x260

Pass in_atomic down to aa_path_perm() and set it for peer checks.

Fixes: c05e705812d1 ("apparmor: add fine grained af_unix mediation")
Cc: stable@vger.kernel.org
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
Seen once on v7.3 (arm64, systemd-detect-virt sending to the journal
socket). It needs an empty buffer pool, so I tested with a test-only
knob that forces the kmalloc path: 189 splats without the patch, 0 with it.

Not fixed here: a chroot_relative profile still reaches path_put() in
d_namespace_path() under the lock (from reading the code, not tested).

Claude (AI) helped write this patch; I reviewed and tested it.
---
 security/apparmor/af_unix.c      | 19 ++++++++++---------
 security/apparmor/file.c         |  5 +++--
 security/apparmor/include/file.h |  3 ++-
 security/apparmor/lsm.c          | 13 +++++++------
 4 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/security/apparmor/af_unix.c b/security/apparmor/af_unix.c
index 58c638830afc..ff851ddd43b4 100644
--- a/security/apparmor/af_unix.c
+++ b/security/apparmor/af_unix.c
@@ -32,7 +32,8 @@ static inline struct sock *aa_unix_sk(struct unix_sock *u)
 }
 
 static int unix_fs_perm(const char *op, u32 mask, const struct cred *subj_cred,
-			struct aa_label *label, const struct path *path)
+			struct aa_label *label, const struct path *path,
+			bool in_atomic)
 {
 	AA_BUG(!label);
 	AA_BUG(!path);
@@ -54,7 +55,7 @@ static int unix_fs_perm(const char *op, u32 mask, const struct cred *subj_cred,
 		};
 
 		return aa_path_perm(op, subj_cred, label, path,
-				    PATH_SOCK_COND, mask, &cond);
+				    PATH_SOCK_COND, mask, &cond, in_atomic);
 	} /* else implicitly delegated */
 
 	return 0;
@@ -240,7 +241,7 @@ static int profile_sk_perm(struct aa_profile *profile,
 		if (is_unix_fs(sk))
 			return unix_fs_perm(ad->op, request, ad->subj_cred,
 					    &profile->label,
-					    &unix_sk(sk)->path);
+					    &unix_sk(sk)->path, false);
 
 		state = match_to_sk(rules->policy, state, request, unix_sk(sk),
 				    &p, &ad->info);
@@ -302,7 +303,7 @@ static int profile_listen_perm(struct aa_profile *profile, struct sock *sk,
 		if (is_unix_fs(sk))
 			return unix_fs_perm(ad->op, AA_MAY_LISTEN,
 					    ad->subj_cred, &profile->label,
-					    &unix_sk(sk)->path);
+					    &unix_sk(sk)->path, false);
 
 		state = match_to_cmd(rules->policy, state, AA_MAY_LISTEN,
 				     unix_sk(sk), CMD_LISTEN, &p, &ad->info);
@@ -337,7 +338,7 @@ static int profile_accept_perm(struct aa_profile *profile,
 		if (is_unix_fs(sk))
 			return unix_fs_perm(ad->op, AA_MAY_ACCEPT,
 					    ad->subj_cred, &profile->label,
-					    &unix_sk(sk)->path);
+					    &unix_sk(sk)->path, false);
 
 		state = match_to_sk(rules->policy, state, AA_MAY_ACCEPT,
 				    unix_sk(sk), &p, &ad->info);
@@ -368,7 +369,7 @@ static int profile_opt_perm(struct aa_profile *profile, u32 request,
 		if (is_unix_fs(sk))
 			return unix_fs_perm(ad->op, request,
 					    ad->subj_cred, &profile->label,
-					    &unix_sk(sk)->path);
+					    &unix_sk(sk)->path, false);
 
 		state = match_to_cmd(rules->policy, state, request, unix_sk(sk),
 				     CMD_OPT, &p, &ad->info);
@@ -409,10 +410,10 @@ static int profile_peer_perm(struct aa_profile *profile, u32 request,
 
 		if (peer_path)
 			return unix_fs_perm(ad->op, request, ad->subj_cred,
-					    &profile->label, peer_path);
+					    &profile->label, peer_path, true);
 		else if (path)
 			return unix_fs_perm(ad->op, request, ad->subj_cred,
-					    &profile->label, path);
+					    &profile->label, path, true);
 		state = match_to_peer(rules->policy, state, request,
 				      unix_sk(sk),
 				      peer_addr, peer_addrlen, &p, &ad->info);
@@ -775,7 +776,7 @@ int aa_unix_file_perm(const struct cred *subj_cred, struct aa_label *label,
 
 			last_error(error,
 				   unix_fs_perm(op, request, subj_cred, label,
-						&peer_path));
+						&peer_path, false));
 			if (peer_path.dentry)
 				path_put(&peer_path);
 		} else {
diff --git a/security/apparmor/file.c b/security/apparmor/file.c
index baca370ddfc0..056b670894c5 100644
--- a/security/apparmor/file.c
+++ b/security/apparmor/file.c
@@ -281,13 +281,14 @@ static int profile_path_perm(const char *op, const struct cred *subj_cred,
  * @flags: any additional path flags beyond what the profile specifies
  * @request: requested permissions
  * @cond: conditional info for this request  (NOT NULL)
+ * @in_atomic: whether allocations need to be done in atomic context
  *
  * Returns: %0 else error if access denied or other error
  */
 int aa_path_perm(const char *op, const struct cred *subj_cred,
 		 struct aa_label *label,
 		 const struct path *path, int flags, u32 request,
-		 struct path_cond *cond)
+		 struct path_cond *cond, bool in_atomic)
 {
 	struct aa_perms perms = {};
 	struct aa_profile *profile;
@@ -296,7 +297,7 @@ int aa_path_perm(const char *op, const struct cred *subj_cred,
 
 	flags |= PATH_DELEGATE_DELETED | (S_ISDIR(cond->mode) ? PATH_IS_DIR :
 								0);
-	buffer = aa_get_buffer(false);
+	buffer = aa_get_buffer(in_atomic);
 	if (!buffer)
 		return -ENOMEM;
 	error = fn_for_each_confined(label, profile,
diff --git a/security/apparmor/include/file.h b/security/apparmor/include/file.h
index 1614c07fc53e..aee493604333 100644
--- a/security/apparmor/include/file.h
+++ b/security/apparmor/include/file.h
@@ -90,7 +90,8 @@ int __aa_path_perm(const char *op, const struct cred *subj_cred,
 		   struct aa_perms *perms);
 int aa_path_perm(const char *op, const struct cred *subj_cred,
 		 struct aa_label *label, const struct path *path,
-		 int flags, u32 request, struct path_cond *cond);
+		 int flags, u32 request, struct path_cond *cond,
+		 bool in_atomic);
 
 int aa_path_link(const struct cred *subj_cred, struct aa_label *label,
 		 struct dentry *old_dentry, const struct path *new_dir,
diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index eaebc3c60c5a..8224529cfbba 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -229,7 +229,7 @@ static int common_perm(const char *op, const struct path *path, u32 mask,
 	label = __begin_current_label_crit_section(&needput);
 	if (!unconfined(label))
 		error = aa_path_perm(op, current_cred(), label, path, 0, mask,
-				     cond);
+				     cond, false);
 	__end_current_label_crit_section(label, needput);
 
 	return error;
@@ -421,12 +421,12 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_d
 					     label, &new_path, 0,
 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
 					     AA_MAY_SETATTR | AA_MAY_DELETE,
-					     &cond_exchange);
+					     &cond_exchange, false);
 			if (!error)
 				error = aa_path_perm(OP_RENAME_DEST, current_cred(),
 						     label, &old_path,
 						     0, MAY_WRITE | AA_MAY_SETATTR |
-						     AA_MAY_CREATE, &cond_exchange);
+						     AA_MAY_CREATE, &cond_exchange, false);
 		}
 
 		if (!error)
@@ -434,12 +434,12 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_d
 					     label, &old_path, 0,
 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
 					     AA_MAY_SETATTR | AA_MAY_DELETE,
-					     &cond);
+					     &cond, false);
 		if (!error)
 			error = aa_path_perm(OP_RENAME_DEST, current_cred(),
 					     label, &new_path,
 					     0, MAY_WRITE | AA_MAY_SETATTR |
-					     AA_MAY_CREATE, &cond);
+					     AA_MAY_CREATE, &cond, false);
 
 	}
 	end_current_label_crit_section(label, needput);
@@ -497,7 +497,8 @@ static int apparmor_file_open(struct file *file)
 
 		error = aa_path_perm(OP_OPEN, file->f_cred,
 				     label, &file->f_path, 0,
-				     aa_map_file_to_perms(file), &cond);
+				     aa_map_file_to_perms(file), &cond,
+				     false);
 		/* todo cache full allowed permissions set and state */
 		fctx->allow = aa_map_file_to_perms(file);
 	}

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261121-apparmor-unix-peer-5cd3efd6b0a5

Best regards,
--  
Arun Iyengar <aiyengar1990@gmail.com>



                 reply	other threads:[~2026-11-21 21:29 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=20261121-apparmor-unix-peer-v1-1-99dd1cdb1e27@gmail.com \
    --to=devnull+aiyengar1990.gmail.com@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=apparmor@lists.ubuntu.com \
    --cc=georgia.garcia@canonical.com \
    --cc=jmorris@namei.org \
    --cc=john.johansen@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=serge@hallyn.com \
    --cc=stable@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®