From mboxrd@z Thu Jan 1 00:00:00 1970 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 D285EC3DA66 for ; Fri, 25 Aug 2023 15:01:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245385AbjHYPAm (ORCPT ); Fri, 25 Aug 2023 11:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343891AbjHYPAd (ORCPT ); Fri, 25 Aug 2023 11:00:33 -0400 Received: from smtp-bc0e.mail.infomaniak.ch (smtp-bc0e.mail.infomaniak.ch [IPv6:2001:1600:4:17::bc0e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 757922126 for ; Fri, 25 Aug 2023 08:00:31 -0700 (PDT) Received: from smtp-3-0001.mail.infomaniak.ch (unknown [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4RXNRd51XGzMrDCp; Fri, 25 Aug 2023 15:00:29 +0000 (UTC) Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4RXNRb5dkbzMpp9w; Fri, 25 Aug 2023 17:00:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1692975629; bh=MWLOkIhU3cieHE64ijsQJbZNvf/Q/VzrZFdssdSQjKc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=y+ZeYvz28WWNongStm5hgbg6AHbg7VkeNBZtvNctX7lxyL5OBaAEvtzx/j81YghU8 9CXjUZLgbiz0HBzcd7Z65EArbgm5/gY8Ykou6xLcwrZod/JpUGMyoka/iU4+/2zmeF AUj1i/cyJPzocJ2pI+kNXBuwQl4OG8NChoP4+wPk= Date: Fri, 25 Aug 2023 17:00:24 +0200 From: =?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?= To: Casey Schaufler Cc: paul@paul-moore.com, linux-security-module@vger.kernel.org, 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, selinux@vger.kernel.org Subject: Re: [PATCH v13 10/11] SELinux: Add selfattr hooks Message-ID: <20230825.iQuae1shaifo@digikod.net> References: <20230802174435.11928-1-casey@schaufler-ca.com> <20230802174435.11928-11-casey@schaufler-ca.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230802174435.11928-11-casey@schaufler-ca.com> X-Infomaniak-Routing: alpha Precedence: bulk List-ID: X-Mailing-List: linux-api@vger.kernel.org On Wed, Aug 02, 2023 at 10:44:33AM -0700, Casey Schaufler wrote: > 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: selinux@vger.kernel.org > Cc: Paul Moore > --- > security/selinux/hooks.c | 136 +++++++++++++++++++++++++++++++-------- > 1 file changed, 109 insertions(+), 27 deletions(-) > +static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx, > + size_t *size, u32 flags) > +{ > + char *value; > + size_t total_len; > + int len; > + int rc; > + > + len = selinux_lsm_getattr(attr, current, &value); > + if (len < 0) > + return len; > + > + total_len = ALIGN(struct_size(ctx, ctx, len), 8); > + > + if (total_len > *size) It looks a bit weird that size must be greater than all the LSM attributes even when ctx is NULL. Same for other getselfattr hook implementations. > + rc = -E2BIG; > + else if (ctx) > + rc = lsm_fill_user_ctx(ctx, value, len, LSM_ID_SELINUX, 0); > + else > + rc = 1; Agreed with Paul, we should initialize rc to zero. > + > + kfree(value); > + *size = total_len; > + if (rc < 0) > + return rc; > + return 1; > +} > + > +static int selinux_setselfattr(unsigned int __user attr, struct lsm_ctx *ctx, > + size_t __user size, u32 __user flags) This __user attribute is dedicated to user pointers, not values. > +{ > + int rc; > + Good to see this refactoring! > + rc = selinux_lsm_setattr(attr, ctx->ctx, ctx->ctx_len); > + if (rc > 0) > + return 0; > + return rc; > +} > + > +static int selinux_getprocattr(struct task_struct *p, > + const char *name, char **value) > +{ > + unsigned int attr = lsm_name_to_attr(name); > + int rc; > + > + if (attr) { > + rc = selinux_lsm_getattr(attr, p, value); > + if (rc != -EOPNOTSUPP) > + return rc; > + } > + > + return -EINVAL; > +} > + > +static int selinux_setprocattr(const char *name, void *value, size_t size) > +{ > + int attr = lsm_name_to_attr(name); > + > + if (attr) > + return selinux_lsm_setattr(attr, value, size); > + return -EINVAL; > +} > + > static int selinux_ismaclabel(const char *name) > { > return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); > @@ -7080,6 +7160,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { > > LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), > > + LSM_HOOK_INIT(getselfattr, selinux_getselfattr), > + LSM_HOOK_INIT(setselfattr, selinux_setselfattr), > LSM_HOOK_INIT(getprocattr, selinux_getprocattr), > LSM_HOOK_INIT(setprocattr, selinux_setprocattr), > > -- > 2.41.0 >