From nobody Tue Dec 16 17:01:00 2025 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 51234C7618E for ; Fri, 21 Apr 2023 17:47:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233450AbjDURq6 (ORCPT ); Fri, 21 Apr 2023 13:46:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232766AbjDURqv (ORCPT ); Fri, 21 Apr 2023 13:46:51 -0400 Received: from sonic305-27.consmr.mail.ne1.yahoo.com (sonic305-27.consmr.mail.ne1.yahoo.com [66.163.185.153]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B485E2710 for ; Fri, 21 Apr 2023 10:46:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1682099189; bh=0NTUeNzMGaXMn+H8CGAQVpJ+M1ACmhxnQPQXiZxaLKk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=DqH7U5fl4CeogDUUHgW55FUZXpevXO3CnGZgqABKWWn5ZcD0HVkvSwoLnO1xmoKLgCCgtG5Ohd8QMb0i+9Muu1d0DPwhN31voRPtBoXDvGLu0vhQIjfI0mM2Vq2IYHyoIL/3Eo5gk+PVSpfBPxkHchdeiDiTP/AZg5q3yodye3qZJCzgcDzSPyrnli/THnP2iwcdTB+JzzHhwMqqid9rG2MdMXTfIzLsSBeuL+IPqHmKlTjpmF2z4/QbmLlK3S+bz7qwmVYqj/2h/6WzIpuZtGNjEgOdZDdYnacoviWcQG74TkmfXtQ/fAR0EYS1qCg8TuMOlopPrpNJgnooHvg5BQ== X-SONIC-DKIM-SIGN: v=1; a=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joZY QrWBLrkmHy2rl5hevUcju5_UKCS2XANLk3bGylNWNs5ggzm8l6Ki3hIlLfkrHD14iu6uB6tDlyEj 4owsDDg1adbElFEq092ebcA6qgnYYEqtGL9etEH9CwbvM9j11QgZhrndaz7jJ3BRmhwmCCwiCvya ehEV_OEdSJsDHCzzU151LEqsNPD1eokvFwrtU4GH6nvtdo6GASOzphUJhfgtjy9aPxDRql2Kc.KB TczOnQGdbh7O1SksjPXMct4YXhTFmJGQURmu84g_HyksPPSGj4RUkJmEK4Sl7UJF2JwTfkfZ49h8 qAi0kS66hEzzC0KiRnrc050qZ_D0YHP4JQl_D3iwVocalJjqMlJ8_FWHxlVJXp.SMhuTJ95R0H6h gH5p6_5OduvnGzSYua7qG.MoM6pLu_vIrV9Q0ahKpA1dVCjFUFMMs77ReRGQUqMx7QURokyk8K9W 8XvtLT6oFl7EudOJjDNGLYnbo0AoMQEdkw.5BOT_inu033XzSZJQStXcJKXaNi1pBSqAfttPc3J4 cNiWc1Y4K6MfhBunrN3zk.FFL.Gxv1_I496eWUXU0SjyWuabeX.wPs0OBsMGI0AtTjQOPz1lHPuQ aMbgpbOt.tQEPK64m.BdZsN13wna0YbKmbCG65AqmpNi.FJwDtAc2_kfG8ZqkHsAYcCweaaN7ZLl YDSVDjld6z1AYeAovVsSs X-Sonic-MF: X-Sonic-ID: 5b4ad7b4-8deb-4b10-ab81-eb15e54dd0bb Received: from sonic.gate.mail.ne1.yahoo.com by sonic305.consmr.mail.ne1.yahoo.com with HTTP; Fri, 21 Apr 2023 17:46:29 +0000 Received: by hermes--production-gq1-546798879c-l2qgj (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID b7487fe0c5844b066c0cbb2b5e828d88; Fri, 21 Apr 2023 17:46:24 +0000 (UTC) From: Casey Schaufler To: casey@schaufler-ca.com, paul@paul-moore.com, linux-security-module@vger.kernel.org Cc: jmorris@namei.org, 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 v9 08/11] Smack: implement setselfattr and getselfattr hooks Date: Fri, 21 Apr 2023 10:42:56 -0700 Message-Id: <20230421174259.2458-9-casey@schaufler-ca.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230421174259.2458-1-casey@schaufler-ca.com> References: <20230421174259.2458-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" Implement Smack support for security_[gs]etselfattr. Refactor the setprocattr hook to avoid code duplication. Signed-off-by: Casey Schaufler --- security/smack/smack_lsm.c | 105 +++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 5 deletions(-) diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 3cf862fcbe08..902b39c187bf 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -3552,6 +3552,41 @@ static void smack_d_instantiate(struct dentry *opt_d= entry, struct inode *inode) return; } =20 +/** + * smack_getselfattr - Smack current process attribute + * @attr: which attribute to fetch + * @ctx: buffer to receive the result + * @size: available size in, actual size out + * @flags: unused + * + * Fill the passed user space @ctx with the details of the requested + * attribute. + * + * Returns 1, the number of attributes, on success, an error code otherwis= e. + */ +static int smack_getselfattr(unsigned int __user attr, + struct lsm_ctx __user *ctx, size_t *size, + u32 __user flags) +{ + struct smack_known *skp =3D smk_of_current(); + int total; + int slen; + int rc =3D 1; + + if (attr !=3D LSM_ATTR_CURRENT) + return -EOPNOTSUPP; + + slen =3D strlen(skp->smk_known) + 1; + total =3D ALIGN(slen + sizeof(*ctx), 8); + if (total > *size) + rc =3D -E2BIG; + else + lsm_fill_user_ctx(ctx, skp->smk_known, slen, LSM_ID_SMACK, 0); + + *size =3D total; + return rc; +} + /** * smack_getprocattr - Smack process attribute access * @p: the object task @@ -3581,8 +3616,8 @@ static int smack_getprocattr(struct task_struct *p, c= onst char *name, char **val } =20 /** - * smack_setprocattr - Smack process attribute setting - * @name: the name of the attribute in /proc/.../attr + * do_setattr - Smack process attribute setting + * @attr: the ID of the attribute * @value: the value to set * @size: the size of the value * @@ -3591,7 +3626,7 @@ static int smack_getprocattr(struct task_struct *p, c= onst char *name, char **val * * Returns the length of the smack label or an error code */ -static int smack_setprocattr(const char *name, void *value, size_t size) +static int do_setattr(u64 attr, void *value, size_t size) { struct task_smack *tsp =3D smack_cred(current_cred()); struct cred *new; @@ -3605,8 +3640,8 @@ static int smack_setprocattr(const char *name, void *= value, size_t size) if (value =3D=3D NULL || size =3D=3D 0 || size >=3D SMK_LONGLABEL) return -EINVAL; =20 - if (strcmp(name, "current") !=3D 0) - return -EINVAL; + if (attr !=3D LSM_ATTR_CURRENT) + return -EOPNOTSUPP; =20 skp =3D smk_import_entry(value, size); if (IS_ERR(skp)) @@ -3645,6 +3680,64 @@ static int smack_setprocattr(const char *name, void = *value, size_t size) return size; } =20 +/** + * smack_setselfattr - Set a Smack process attribute + * @attr: which attribute to set + * @ctx: buffer containing the data + * @size: size of @ctx + * @flags: unused + * + * Fill the passed user space @ctx with the details of the requested + * attribute. + * + * Returns 0 on success, an error code otherwise. + */ +static int smack_setselfattr(unsigned int __user attr, + struct lsm_ctx __user *ctx, size_t __user size, + u32 __user flags) +{ + struct lsm_ctx *lctx; + void *context; + int rc; + + context =3D kmalloc(size, GFP_KERNEL); + if (context =3D=3D NULL) + return -ENOMEM; + + lctx =3D (struct lsm_ctx *)context; + if (copy_from_user(context, ctx, size)) + rc =3D -EFAULT; + else if (lctx->ctx_len > size) + rc =3D -EINVAL; + else + rc =3D do_setattr(attr, lctx + 1, lctx->ctx_len); + + kfree(context); + if (rc > 0) + return 0; + return rc; +} + +/** + * smack_setprocattr - Smack process attribute setting + * @name: the name of the attribute in /proc/.../attr + * @value: the value to set + * @size: the size of the value + * + * Sets the Smack value of the task. Only setting self + * is permitted and only with privilege + * + * Returns the length of the smack label or an error code + */ +static int smack_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; +} + /** * smack_unix_stream_connect - Smack access on UDS * @sock: one sock @@ -4955,6 +5048,8 @@ static struct security_hook_list smack_hooks[] __lsm_= ro_after_init =3D { =20 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate), =20 + LSM_HOOK_INIT(getselfattr, smack_getselfattr), + LSM_HOOK_INIT(setselfattr, smack_setselfattr), LSM_HOOK_INIT(getprocattr, smack_getprocattr), LSM_HOOK_INIT(setprocattr, smack_setprocattr), =20 --=20 2.39.2