From: "Serge Hallyn (shallyn)" <shallyn@cisco.com>
To: Casey Schaufler <casey@schaufler-ca.com>
Cc: "casey.schaufler@intel.com" <casey.schaufler@intel.com>,
"paul@paul-moore.com" <paul@paul-moore.com>,
"linux-security-module@vger.kernel.org"
<linux-security-module@vger.kernel.org>,
"jmorris@namei.org" <jmorris@namei.org>,
"keescook@chromium.org" <keescook@chromium.org>,
"john.johansen@canonical.com" <john.johansen@canonical.com>,
"penguin-kernel@i-love.sakura.ne.jp"
<penguin-kernel@i-love.sakura.ne.jp>,
"stephen.smalley.work@gmail.com" <stephen.smalley.work@gmail.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-api@vger.kernel.org" <linux-api@vger.kernel.org>,
"mic@digikod.net" <mic@digikod.net>
Subject: Re: [PATCH v5 4/8] LSM: lsm_get_self_attr syscall for LSM self attributes
Date: Thu, 2 Feb 2023 04:53:54 +0000 [thread overview]
Message-ID: <Y9tB4PAzizLOV+li@jerom> (raw)
In-Reply-To: <20230109180717.58855-5-casey@schaufler-ca.com>
On Mon, Jan 09, 2023 at 10:07:13AM -0800, Casey Schaufler wrote:
> +/**
> + * sys_lsm_get_self_attr - Return current task's security module attributes
> + * @ctx: the LSM contexts
> + * @size: size of @ctx, updated on return
> + * @flags: which attribute to return
> + *
> + * Returns the calling task's LSM contexts. On success this
> + * function returns the number of @ctx array elements. This value
> + * may be zero if there are no LSM contexts assigned. If @size is
> + * insufficient to contain the return data -E2BIG is returned and
Technicality: You say -E2BIG, which is -7, but in fact belog you return
-ERANGE, which is -34.
> + * @size is set to the minimum required size. In all other cases
> + * a negative value indicating the error is returned.
> + */
> +SYSCALL_DEFINE3(lsm_get_self_attr,
> + struct lsm_ctx __user *, ctx,
> + size_t __user *, size,
> + u32, flags)
> +{
> + int i;
> + int rc = 0;
> + int len;
> + int attr;
> + int count = 0;
> + void *curr;
> + char *cp;
> + char *np;
> + char **interum_ctx;
> + size_t total_size = 0;
> + struct lsm_ctx *ip;
> + struct lsm_ctx *interum;
> + struct lsm_ctx *final = NULL;
> +
> + attr = attr_used_index(flags);
> + if (attr < 0)
> + return attr;
> +
> + interum = kzalloc(ARRAY_SIZE(lsm_attr_names) * lsm_active_cnt *
> + sizeof(*interum), GFP_KERNEL);
> + if (interum == NULL)
> + return -ENOMEM;
> + ip = interum;
> +
> + interum_ctx = kzalloc(ARRAY_SIZE(lsm_attr_names) * lsm_active_cnt *
> + sizeof(*interum_ctx), GFP_KERNEL);
> + if (interum_ctx == NULL) {
> + kfree(interum);
> + return -ENOMEM;
> + }
> +
> + for (i = 0; i < lsm_active_cnt; i++) {
> + if ((lsm_idlist[i]->attrs_used &
> + lsm_attr_names[attr].attrs_used) == 0)
> + continue;
> +
> + len = security_getprocattr(current, lsm_idlist[i]->id,
> + lsm_attr_names[attr].name,
> + &cp);
> + if (len <= 0)
> + continue;
> +
> + ip->id = lsm_idlist[i]->id;
> + ip->flags = lsm_attr_names[attr].attrs_used;
> + interum_ctx[count] = cp;
> +
> + /*
> + * A security module that returns a binary attribute
> + * will need to identify itself to prevent string
> + * processing.
> + *
> + * At least one security module adds a \n at the
> + * end of a context to make it look nicer. Change
> + * that to a \0 so that user space doesn't have to
> + * work around it.
> + *
> + * Security modules have been inconsistent about
> + * including the \0 terminator in the size. If it's
> + * not there make space for it.
> + *
> + * The length returned will reflect the length of
> + * the string provided by the security module, which
> + * may not match what getprocattr returned.
> + */
> + np = strnchr(cp, len, '\n');
> + if (np != NULL)
> + *np = '\0';
> + ip->ctx_len = strnlen(cp, len) + 1;
> + total_size += sizeof(*interum) + ip->ctx_len;
> + ip++;
> + count++;
> + }
> +
> + if (count == 0)
> + goto free_out;
> +
> + final = kzalloc(total_size, GFP_KERNEL);
> + if (final == NULL) {
> + rc = -ENOMEM;
> + goto free_out;
> + }
> +
> + curr = final;
> + ip = interum;
> + for (i = 0; i < count; i++) {
> + memcpy(curr, ip, sizeof(*interum));
> + curr += sizeof(*interum);
> + if (ip->ctx_len > 1)
> + memcpy(curr, interum_ctx[i], ip->ctx_len - 1);
> + curr += ip->ctx_len;
> + ip++;
> + }
> +
> + if (get_user(len, size)) {
> + rc = -EFAULT;
> + goto free_out;
> + }
> + if (total_size > len) {
> + rc = -ERANGE;
> + if (put_user(total_size, size) != 0)
> + rc = -EFAULT;
> + goto free_out;
> + }
> + if (copy_to_user(ctx, final, total_size) != 0 ||
> + put_user(total_size, size) != 0)
> + rc = -EFAULT;
> + else
> + rc = count;
> +
> +free_out:
> + for (i = 0; i < count; i++)
> + kfree(interum_ctx[i]);
> + kfree(interum_ctx);
> + kfree(interum);
> + kfree(final);
> + return rc;
> +}
> --
> 2.39.0
>
next prev parent reply other threads:[~2023-02-02 5:01 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20230109180717.58855-1-casey.ref@schaufler-ca.com>
2023-01-09 18:07 ` [PATCH v5 0/8] LSM: Three basic syscalls Casey Schaufler
2023-01-09 18:07 ` [PATCH v5 1/8] LSM: Identify modules by more than name Casey Schaufler
2023-01-11 21:00 ` Paul Moore
2023-01-12 0:05 ` Casey Schaufler
2023-01-12 20:30 ` Paul Moore
2023-01-09 18:07 ` [PATCH v5 2/8] LSM: Maintain a table of LSM attribute data Casey Schaufler
2023-01-11 21:01 ` Paul Moore
2023-01-12 0:36 ` Casey Schaufler
2023-01-12 20:26 ` Paul Moore
2023-01-09 18:07 ` [PATCH v5 3/8] proc: Use lsmids instead of lsm names for attrs Casey Schaufler
2023-01-11 21:01 ` Paul Moore
2023-01-12 0:37 ` Casey Schaufler
2023-01-09 18:07 ` [PATCH v5 4/8] LSM: lsm_get_self_attr syscall for LSM self attributes Casey Schaufler
2023-01-11 21:07 ` Paul Moore
2023-01-12 1:37 ` Casey Schaufler
2023-01-12 21:37 ` Paul Moore
2023-01-12 14:40 ` Arnd Bergmann
2023-01-12 21:39 ` Paul Moore
2023-02-14 16:48 ` Mickaël Salaün
2023-02-02 4:53 ` Serge Hallyn (shallyn) [this message]
2023-02-14 17:41 ` Mickaël Salaün
2023-02-14 18:06 ` Casey Schaufler
2023-01-09 18:07 ` [PATCH v5 5/8] LSM: Create lsm_module_list system call Casey Schaufler
2023-01-11 21:07 ` Paul Moore
2023-01-12 1:39 ` Casey Schaufler
2023-01-12 21:43 ` Paul Moore
2023-01-09 18:07 ` [PATCH v5 6/8] LSM: lsm_set_self_attr syscall for LSM self attributes Casey Schaufler
2023-01-09 18:07 ` [PATCH v5 7/8] LSM: wireup Linux Security Module syscalls Casey Schaufler
2023-01-13 9:31 ` Geert Uytterhoeven
2023-01-09 18:07 ` [PATCH v5 8/8] LSM: selftests for " Casey Schaufler
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=Y9tB4PAzizLOV+li@jerom \
--to=shallyn@cisco.com \
--cc=casey.schaufler@intel.com \
--cc=casey@schaufler-ca.com \
--cc=jmorris@namei.org \
--cc=john.johansen@canonical.com \
--cc=keescook@chromium.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=mic@digikod.net \
--cc=paul@paul-moore.com \
--cc=penguin-kernel@i-love.sakura.ne.jp \
--cc=stephen.smalley.work@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).