From nobody Mon Feb 9 13:36:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66CF2EB64D9 for ; Thu, 29 Jun 2023 20:01:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232592AbjF2UBL (ORCPT ); Thu, 29 Jun 2023 16:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232434AbjF2UAj (ORCPT ); Thu, 29 Jun 2023 16:00:39 -0400 Received: from sonic311-30.consmr.mail.ne1.yahoo.com (sonic311-30.consmr.mail.ne1.yahoo.com [66.163.188.211]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A2FA35B0 for ; Thu, 29 Jun 2023 13:00:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1688068834; bh=FecLkrXox/1UtDMuj0YVSfxHtwMaEWW93HEIoUhvMVY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=eXo+5TKvNKfwg1J3yxpHvu7BjkAs8LVtruGj65k7/BFH8A4GnorEiz7Njw0qcX50MXgrjahRrMKEt2LXiiZXSNLhSdIoEqn4VcsxuW6UvB5W7N+c3IQd2hZwJiLCQXC/GQqbG+TBo0do4GWjvM6hozJ2zTXwRhgVi8HtwmUE/03/w0vmblpgsv7Y/UKSqaDrULysEM/hnw+pfIbIu+aFyq3MaIfBxMfwZl5jNdAnj2Gm1fyNGvMiixNeFJ/upyaaJavTX4mhJMhf1oCgtmdQqiwUFqCe/BPP8O3mgBa6+ZRQoDxp7cpYBBcy4SQPo4Zy6oHYUoip/BK5wS2GeqjECg== X-SONIC-DKIM-SIGN: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KaiF51QgNoqDB7HcvJ6UQCX4YYYZTpSIXyYVLvS7Yi6DUVoPoEZUjtp8xjg185Pj Tr4XALtQFgcPf7FqN_nMf9NVCqzoafDHApsEN4U27IFTSRgdXZirJWF5H3GdrT8cgRizQtLamAb7 V24OAeTSLHzB65BHNvdSLS8Dhgb.j9dKLWWvHR6ZmqlGqnDMOZuNLzS5KdoCbFM7viekVOStSt.a so9Gey.UnTJe31lwyR_JIf_1SxLmI8z97E_aeroRlB5zX5F0R3JyfBIicz4D6iS2MJP__wz0YdWS EpOAfqlDXHDDLYnnFW.DuLzxG2jlrjb6ISY0T3VwmgIH7J82PJ4GQpYmt880HPJkp3ocNQOGICcj RK12vFDXl0kw0lw5UfoDQ_F7Pp1nspmjYFxtKmStwizX0PCp4JV0yZssl0LJ5tlHaomBdMqYJZJN wVQSN9QN0YENj3IStNlr7a8fv54QcM.tNNU8suFzOcNDYXtIxzZ2epmfyW9kYC3Q02lRqWVBpAo7 BA1uDj1gudMS2UrrofywxZQ55Gx1W69xS91IuEetqdkWPdohnBIBbNsjJAQttbPOQoK_HQIn6M3m y.ly4ellfHtFOGvHgnNu4ED7ZmogWvoFa2xT_JXb1TlTPDYtB3e5_..3rW0LqcAIUuw5YKiQFJo6 nx3F6hRBmevnT6P0V05follZpLYQ- X-Sonic-MF: X-Sonic-ID: a34986ca-e6ee-49fb-a32b-5d35c826c077 Received: from sonic.gate.mail.ne1.yahoo.com by sonic311.consmr.mail.ne1.yahoo.com with HTTP; Thu, 29 Jun 2023 20:00:34 +0000 Received: by hermes--production-ne1-6d679867d5-5vpxd (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 3de0d904516dfd44a291f116baddfae9; Thu, 29 Jun 2023 20:00:30 +0000 (UTC) From: Casey Schaufler To: casey@schaufler-ca.com, paul@paul-moore.com, linux-security-module@vger.kernel.org Cc: jmorris@namei.org, serge@hallyn.com, keescook@chromium.org, john.johansen@canonical.com, penguin-kernel@i-love.sakura.ne.jp, stephen.smalley.work@gmail.com, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, mic@digikod.net Subject: [PATCH v12 09/11] AppArmor: Add selfattr hooks Date: Thu, 29 Jun 2023 12:55:33 -0700 Message-Id: <20230629195535.2590-10-casey@schaufler-ca.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230629195535.2590-1-casey@schaufler-ca.com> References: <20230629195535.2590-1-casey@schaufler-ca.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add hooks for setselfattr and getselfattr. These hooks are not very different from their setprocattr and getprocattr equivalents, and much of the code is shared. Signed-off-by: Casey Schaufler Cc: John Johansen --- security/apparmor/include/procattr.h | 2 +- security/apparmor/lsm.c | 102 +++++++++++++++++++++++++-- security/apparmor/procattr.c | 10 +-- 3 files changed, 103 insertions(+), 11 deletions(-) diff --git a/security/apparmor/include/procattr.h b/security/apparmor/inclu= de/procattr.h index 31689437e0e1..03dbfdb2f2c0 100644 --- a/security/apparmor/include/procattr.h +++ b/security/apparmor/include/procattr.h @@ -11,7 +11,7 @@ #ifndef __AA_PROCATTR_H #define __AA_PROCATTR_H =20 -int aa_getprocattr(struct aa_label *label, char **string); +int aa_getprocattr(struct aa_label *label, char **string, bool newline); int aa_setprocattr_changehat(char *args, size_t size, int flags); =20 #endif /* __AA_PROCATTR_H */ diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index 308cb3a281c3..7d99d5be4c12 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -630,6 +630,54 @@ static int apparmor_sb_pivotroot(const struct path *ol= d_path, return error; } =20 +static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *= lx, + size_t *size, u32 flags) +{ + int error =3D -ENOENT; + struct aa_task_ctx *ctx =3D task_ctx(current); + struct aa_label *label =3D NULL; + size_t total_len =3D 0; + char *value; + + switch (attr) { + case LSM_ATTR_CURRENT: + label =3D aa_get_newest_label(cred_label(current_cred())); + break; + case LSM_ATTR_PREV: + if (ctx->previous) + label =3D aa_get_newest_label(ctx->previous); + break; + case LSM_ATTR_EXEC: + if (ctx->onexec) + label =3D aa_get_newest_label(ctx->onexec); + break; + default: + error =3D -EOPNOTSUPP; + break; + } + + if (label) { + error =3D aa_getprocattr(label, &value, false); + if (error > 0) { + total_len =3D ALIGN(struct_size(lx, ctx, error), 8); + if (total_len > *size) + error =3D -E2BIG; + else if (lx) + error =3D lsm_fill_user_ctx(lx, value, error, + LSM_ID_APPARMOR, 0); + else + error =3D 1; + } + } + + aa_put_label(label); + + *size =3D total_len; + if (error < 0) + return error; + return 1; +} + static int apparmor_getprocattr(struct task_struct *task, const char *name, char **value) { @@ -649,7 +697,7 @@ static int apparmor_getprocattr(struct task_struct *tas= k, const char *name, error =3D -EINVAL; =20 if (label) - error =3D aa_getprocattr(label, value); + error =3D aa_getprocattr(label, value, true); =20 aa_put_label(label); put_cred(cred); @@ -657,8 +705,7 @@ static int apparmor_getprocattr(struct task_struct *tas= k, const char *name, return error; } =20 -static int apparmor_setprocattr(const char *name, void *value, - size_t size) +static int do_setattr(u64 attr, void *value, size_t size) { char *command, *largs =3D NULL, *args =3D value; size_t arg_size; @@ -689,7 +736,7 @@ static int apparmor_setprocattr(const char *name, void = *value, goto out; =20 arg_size =3D size - (args - (largs ? largs : (char *) value)); - if (strcmp(name, "current") =3D=3D 0) { + if (attr =3D=3D LSM_ATTR_CURRENT) { if (strcmp(command, "changehat") =3D=3D 0) { error =3D aa_setprocattr_changehat(args, arg_size, AA_CHANGE_NOFLAGS); @@ -704,7 +751,7 @@ static int apparmor_setprocattr(const char *name, void = *value, error =3D aa_change_profile(args, AA_CHANGE_STACK); } else goto fail; - } else if (strcmp(name, "exec") =3D=3D 0) { + } else if (attr =3D=3D LSM_ATTR_EXEC) { if (strcmp(command, "exec") =3D=3D 0) error =3D aa_change_profile(args, AA_CHANGE_ONEXEC); else if (strcmp(command, "stack") =3D=3D 0) @@ -724,13 +771,54 @@ static int apparmor_setprocattr(const char *name, voi= d *value, =20 fail: aad(&sa)->label =3D begin_current_label_crit_section(); - aad(&sa)->info =3D name; + if (attr =3D=3D LSM_ATTR_CURRENT) + aad(&sa)->info =3D "current"; + else if (attr =3D=3D LSM_ATTR_EXEC) + aad(&sa)->info =3D "exec"; + else + aad(&sa)->info =3D "invalid"; aad(&sa)->error =3D error =3D -EINVAL; aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); end_current_label_crit_section(aad(&sa)->label); goto out; } =20 +static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx __user *= ctx, + size_t size, u32 flags) +{ + struct lsm_ctx *lctx; + int rc; + + if (attr !=3D LSM_ATTR_CURRENT && attr !=3D LSM_ATTR_EXEC) + return -EOPNOTSUPP; + + lctx =3D kmalloc(size, GFP_KERNEL); + if (lctx =3D=3D NULL) + return -ENOMEM; + + if (copy_from_user(lctx, ctx, size)) + rc =3D -EFAULT; + else if (lctx->ctx_len > size) + rc =3D -EINVAL; + else + rc =3D do_setattr(attr, lctx->ctx, lctx->ctx_len); + + kfree(lctx); + if (rc > 0) + return 0; + return rc; +} + +static int apparmor_setprocattr(const char *name, void *value, + size_t size) +{ + int attr =3D lsm_name_to_attr(name); + + if (attr) + return do_setattr(attr, value, size); + return -EINVAL; +} + /** * apparmor_bprm_committing_creds - do task cleanup on committing new creds * @bprm: binprm for the exec (NOT NULL) @@ -1253,6 +1341,8 @@ static struct security_hook_list apparmor_hooks[] __r= o_after_init =3D { LSM_HOOK_INIT(file_lock, apparmor_file_lock), LSM_HOOK_INIT(file_truncate, apparmor_file_truncate), =20 + LSM_HOOK_INIT(getselfattr, apparmor_getselfattr), + LSM_HOOK_INIT(setselfattr, apparmor_setselfattr), LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), =20 diff --git a/security/apparmor/procattr.c b/security/apparmor/procattr.c index 197d41f9c32b..e3857e3d7c6c 100644 --- a/security/apparmor/procattr.c +++ b/security/apparmor/procattr.c @@ -20,6 +20,7 @@ * aa_getprocattr - Return the label information for @label * @label: the label to print label info about (NOT NULL) * @string: Returns - string containing the label info (NOT NULL) + * @newline: indicates that a newline should be added * * Requires: label !=3D NULL && string !=3D NULL * @@ -27,7 +28,7 @@ * * Returns: size of string placed in @string else error code on failure */ -int aa_getprocattr(struct aa_label *label, char **string) +int aa_getprocattr(struct aa_label *label, char **string, bool newline) { struct aa_ns *ns =3D labels_ns(label); struct aa_ns *current_ns =3D aa_get_current_ns(); @@ -57,11 +58,12 @@ int aa_getprocattr(struct aa_label *label, char **strin= g) return len; } =20 - (*string)[len] =3D '\n'; - (*string)[len + 1] =3D 0; + if (newline) + (*string)[len++] =3D '\n'; + (*string)[len] =3D 0; =20 aa_put_ns(current_ns); - return len + 1; + return len; } =20 /** --=20 2.40.1