Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1302925rwd; Thu, 15 Jun 2023 08:47:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7LJeZ2C0Vb8RMlUHFQkAKfcH6VW3UKGhvM+jqVTKqHqkqppfzqqjJ49DVt+2qnLHo1q++U X-Received: by 2002:aa7:d99a:0:b0:518:7bd4:4740 with SMTP id u26-20020aa7d99a000000b005187bd44740mr4445405eds.10.1686844073099; Thu, 15 Jun 2023 08:47:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686844073; cv=none; d=google.com; s=arc-20160816; b=QIsZYJGVKDtieJfWigUx+X7plEVhwIGxaaBACsSWHOhWOMFT3sFCHJG1nzY8LoJUJY iQmXvvoz83P05DUqrh6p2T9cYigQ0kctC4jzU3g5O8wO39a7UCXUJqABlg0qQaJrieQA dXbpPPv3Lvf+Ow02CTJ2NVr2tZTgDQBCjjYB6F5PvggH5M7Cj7LGa46n1yus6uLd5FUj VEj6izYBqOgEdXmfUrqmzw9zVO08XGzAM9FCSsg8S0fpXRQrdFUX5NCrqHzTmFw5CEu7 GPlBJ5nM+Vsao+P8uQahqEfh+2gzJm0h4lc45SFeocy3KWhzmVuhnGNIfQxuMiLewIqa 83VQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=rzPJf9+yhJiTnhk6vixUA0/gMY7PnrEAt/rNmqX7Jl4=; b=f0aEpKUmUJeTsV/IEnqJRj6sblnlhr27FhYEA4iVDb8czasYY6r7n5/L5ENIrpIMng +kJ6avAK+rYdYk4p2nVrIhTrAXSOP1tXJufSUYL8qphrIkFI7nix29UXW9NUD/bcg76l KHw0zsq6fBDRHwyHUSQKaSXoqxMC3ASY7nn/UJjm+Hha5WWrZ+WdJSGb9wxCwEd1Q5MN jpKJRubS9QrExhUAruYxRaFpC/0rasxZqNw82s0S6GrUWpYhTmgnv/CUqIVt7zS4s1aD 7CJl++bcf2+m63+L5Wwh230Cz2u+t8tzIHBKkrTO2noWU9Aitgpjd0QHi2rMVe7x52su vOcQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z11-20020a50eb4b000000b00514b21c93f1si643570edp.357.2023.06.15.08.47.27; Thu, 15 Jun 2023 08:47:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244885AbjFOPa5 (ORCPT + 99 others); Thu, 15 Jun 2023 11:30:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344466AbjFOPam (ORCPT ); Thu, 15 Jun 2023 11:30:42 -0400 Received: from mail.hallyn.com (mail.hallyn.com [178.63.66.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F00CB30F8; Thu, 15 Jun 2023 08:30:16 -0700 (PDT) Received: from jerom (unknown [12.133.101.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: serge) by mail.hallyn.com (Postfix) with ESMTPSA id E10D4DD; Thu, 15 Jun 2023 10:29:41 -0500 (CDT) Date: Thu, 15 Jun 2023 10:29:36 -0500 From: Serge Hallyn To: Casey Schaufler Cc: paul@paul-moore.com, linux-security-module@vger.kernel.org, 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: Re: [PATCH v10 08/11] Smack: implement setselfattr and getselfattr hooks Message-ID: References: <20230428203417.159874-1-casey@schaufler-ca.com> <20230428203417.159874-9-casey@schaufler-ca.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230428203417.159874-9-casey@schaufler-ca.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 28, 2023 at 01:34:14PM -0700, Casey Schaufler wrote: > 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 | 106 +++++++++++++++++++++++++++++++++++-- > 1 file changed, 101 insertions(+), 5 deletions(-) > > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c > index 0b466d3b31b7..def13357156a 100644 > --- a/security/smack/smack_lsm.c > +++ b/security/smack/smack_lsm.c > @@ -3552,6 +3552,45 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) > return; > } > > +/** > + * 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 otherwise. > + */ > +static int smack_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx, > + size_t *size, u32 flags) > +{ > + struct smack_known *skp = smk_of_current(); > + int total; > + int slen; > + int rc; > + > + if (attr != LSM_ATTR_CURRENT) > + return -EOPNOTSUPP; > + > + slen = strlen(skp->smk_known) + 1; > + total = ALIGN(slen + sizeof(*ctx), 8); > + if (total > *size) > + rc = -E2BIG; > + else if (ctx) > + rc = lsm_fill_user_ctx(ctx, skp->smk_known, slen, LSM_ID_SMACK, > + 0); > + else > + rc = 1; > + > + *size = total; > + if (rc >= 0) > + return 1; > + return rc; > +} > + > /** > * smack_getprocattr - Smack process attribute access > * @p: the object task > @@ -3581,8 +3620,8 @@ static int smack_getprocattr(struct task_struct *p, const char *name, char **val > } > > /** > - * 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 +3630,7 @@ static int smack_getprocattr(struct task_struct *p, const 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 = smack_cred(current_cred()); > struct cred *new; > @@ -3605,8 +3644,8 @@ static int smack_setprocattr(const char *name, void *value, size_t size) > if (value == NULL || size == 0 || size >= SMK_LONGLABEL) > return -EINVAL; > > - if (strcmp(name, "current") != 0) > - return -EINVAL; > + if (attr != LSM_ATTR_CURRENT) > + return -EOPNOTSUPP; > > skp = smk_import_entry(value, size); > if (IS_ERR(skp)) > @@ -3645,6 +3684,61 @@ static int smack_setprocattr(const char *name, void *value, size_t size) > return size; > } > > +/** > + * 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 attr, struct lsm_ctx __user *ctx, > + size_t size, u32 flags) > +{ > + struct lsm_ctx *lctx; > + int rc; > + > + lctx = kmalloc(size, GFP_KERNEL); > + if (lctx == NULL) > + return -ENOMEM; > + > + if (copy_from_user(lctx, ctx, size)) > + rc = -EFAULT; > + else if (lctx->ctx_len > size) Did you want to do E2BIG instead here? Or is that really seen as invalid usage? > + rc = -EINVAL; > + else > + rc = do_setattr(attr, lctx->ctx, lctx->ctx_len); > + > + kfree(lctx); > + 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 = lsm_name_to_attr(name); > + > + if (attr) If someone one day gets the brilliant idea to change LSM_ATTR_UNDEF to -1, this won't work, so I would suggest changing to either "if (attr != LSM_ATTR_UNDEF)" or defining a valid_lsm_attr() helper and checking its bool return value. > + return do_setattr(attr, value, size); > + return -EINVAL; > +} > + > /** > * smack_unix_stream_connect - Smack access on UDS > * @sock: one sock > @@ -4955,6 +5049,8 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { > > LSM_HOOK_INIT(d_instantiate, smack_d_instantiate), > > + LSM_HOOK_INIT(getselfattr, smack_getselfattr), > + LSM_HOOK_INIT(setselfattr, smack_setselfattr), > LSM_HOOK_INIT(getprocattr, smack_getprocattr), > LSM_HOOK_INIT(setprocattr, smack_setprocattr), > > -- > 2.39.2 >