All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Smalley <sds@tycho.nsa.gov>
To: Casey Schaufler <casey@schaufler-ca.com>,
	casey.schaufler@intel.com, jmorris@namei.org,
	linux-security-module@vger.kernel.org, selinux@vger.kernel.org
Cc: keescook@chromium.org, john.johansen@canonical.com,
	penguin-kernel@i-love.sakura.ne.jp, paul@paul-moore.com
Subject: Re: [PATCH v12 23/25] NET: Add SO_PEERCONTEXT for multiple LSMs
Date: Wed, 18 Dec 2019 15:50:45 -0500	[thread overview]
Message-ID: <a522de22-ba62-a24d-24f7-b69418e7ec0b@tycho.nsa.gov> (raw)
In-Reply-To: <e7aa3b6f-cee1-6277-21dd-77a4db9bbc2b@tycho.nsa.gov>

On 12/18/19 2:12 PM, Stephen Smalley wrote:
> On 12/18/19 1:28 PM, Stephen Smalley wrote:
>> On 12/16/19 5:36 PM, Casey Schaufler wrote:
>>> The getsockopt SO_PEERSEC provides the LSM based security
>>> information for a single module, but for reasons of backward
>>> compatibility cannot include the information for multiple
>>> modules. A new option SO_PEERCONTEXT is added to report the
>>> security "context" of multiple modules using a "compound" format
>>>
>>>          lsm1\0value\0lsm2\0value\0
>>>
>>> This is expected to be used by system services, including dbus-daemon.
>>> The exact format of a compound context has been the subject of
>>> considerable debate. This format was suggested by Simon McVittie,
>>> a dbus maintainer with a significant stake in the format being
>>> usable.
>>>
>>> Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
>>> cc: netdev@vger.kernel.org
>>
>> Requires ack by netdev and linux-api.  A couple of comments below.
> 
> Also, have you tested this new interface?  I may be doing something 
> wrong, but a trivial attempt to use SO_PEERCONTEXT with both SELinux and 
> AppArmor enabled only appeared to return the SELinux portion of the 
> label 
> (selinux\0unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023\0), 
> whereas /proc/self/attr/context returned a compound context (the same 
> but with apparmor\0unconfined\n\0 appended).

Ok, this seems to be a lack of support in AppArmor for saving the peer 
info for unix/local domain sockets, so not your bug.  Doesn't implement 
the necessary hooks.

> 
>>
>>> ---
>>
>>> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
>>> index 2bf82e1cf347..2ae10e7f81a7 100644
>>> --- a/include/linux/lsm_hooks.h
>>> +++ b/include/linux/lsm_hooks.h
>>> @@ -880,8 +880,8 @@
>>>    *    SO_GETPEERSEC.  For tcp sockets this can be meaningful if the
>>>    *    socket is associated with an ipsec SA.
>>>    *    @sock is the local socket.
>>> - *    @optval userspace memory where the security state is to be 
>>> copied.
>>> - *    @optlen userspace int where the module should copy the actual 
>>> length
>>> + *    @optval memory where the security state is to be copied.
>>
>> This is misleading; it suggests that the caller is providing an 
>> allocated buffer into which the security module copies its data. 
>> Instead it is just a pointer to a pointer that is then set by the 
>> security module to a buffer the module allocates.
>>
>>> diff --git a/include/linux/security.h b/include/linux/security.h
>>> index 536db4dbfcbb..b72bb90b1903 100644
>>> --- a/include/linux/security.h
>>> +++ b/include/linux/security.h
>>> @@ -181,7 +181,7 @@ struct lsmblob {
>>>   #define LSMBLOB_NEEDED        -2    /* Slot requested on 
>>> initialization */
>>>   #define LSMBLOB_NOT_NEEDED    -3    /* Slot not requested */
>>>   #define LSMBLOB_DISPLAY        -4    /* Use the "display" slot */
>>> -#define LSMBLOB_FIRST        -5    /* Use the default "display" slot */
>>> +#define LSMBLOB_COMPOUND    -5    /* A compound "display" */
>>
>> I'm puzzled by the removal of LSMBLOB_FIRST by this patch; it suggests 
>> it was never needed in the first place by the patch that introduced 
>> it. But more below.
>>
>>> diff --git a/security/security.c b/security/security.c
>>> index d0b57a7c3b31..1afe245f3246 100644
>>> --- a/security/security.c
>>> +++ b/security/security.c
>>> @@ -723,6 +723,42 @@ static void __init lsm_early_task(struct 
>>> task_struct *task)
>>>           panic("%s: Early task alloc failed.\n", __func__);
>>>   }
>>> +/**
>>> + * append_ctx - append a lsm/context pair to a compound context
>>> + * @ctx: the existing compound context
>>> + * @ctxlen: size of the old context, including terminating nul byte
>>> + * @lsm: new lsm name, nul terminated
>>> + * @new: new context, possibly nul terminated
>>> + * @newlen: maximum size of @new
>>> + *
>>> + * replace @ctx with a new compound context, appending @newlsm and @new
>>> + * to @ctx. On exit the new data replaces the old, which is freed.
>>> + * @ctxlen is set to the new size, which includes a trailing nul byte.
>>> + *
>>> + * Returns 0 on success, -ENOMEM if no memory is available.
>>> + */
>>> +static int append_ctx(char **ctx, int *ctxlen, const char *lsm, char 
>>> *new,
>>> +              int newlen)
>>> +{
>>> +    char *final;
>>> +    int llen;
>>> +
>>> +    llen = strlen(lsm) + 1;
>>> +    newlen = strnlen(new, newlen) + 1;
>>> +
>>> +    final = kzalloc(*ctxlen + llen + newlen, GFP_KERNEL);
>>> +    if (final == NULL)
>>> +        return -ENOMEM;
>>> +    if (*ctxlen)
>>> +        memcpy(final, *ctx, *ctxlen);
>>> +    memcpy(final + *ctxlen, lsm, llen);
>>> +    memcpy(final + *ctxlen + llen, new, newlen);
>>> +    kfree(*ctx);
>>> +    *ctx = final;
>>> +    *ctxlen = *ctxlen + llen + newlen;
>>> +    return 0;
>>> +}
>>
>> You should likely take some precautions against integer overflows in 
>> the above code?
>>
>>> +
>>>   /*
>>>    * Hook list operation macros.
>>>    *
>>> @@ -2164,8 +2200,8 @@ int security_setprocattr(const char *lsm, const 
>>> char *name, void *value,
>>>       hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
>>>           if (lsm != NULL && strcmp(lsm, hp->lsmid->lsm))
>>>               continue;
>>> -        if (lsm == NULL && *display != LSMBLOB_INVALID &&
>>> -            *display != hp->lsmid->slot)
>>> +        if (lsm == NULL && display != NULL &&
>>> +            *display != LSMBLOB_INVALID && *display != hp->lsmid->slot)
>>>               continue;
>>>           return hp->hook.setprocattr(name, value, size);
>>>       }
>>
>> Is this a bug fix that should be folded into the earlier patch that 
>> introduced it?
>>
>>> @@ -2196,7 +2232,7 @@ int security_secid_to_secctx(struct lsmblob 
>>> *blob, struct lsmcontext *cp,
>>>        */
>>>       if (display == LSMBLOB_DISPLAY)
>>>           display = lsm_task_display(current);
>>> -    else if (display == LSMBLOB_FIRST)
>>> +    else if (display == 0)
>>>           display = LSMBLOB_INVALID;
>>>       else if (display < 0) {
>>>           WARN_ONCE(true,
>>
>> Why is it necessary to re-map display 0 in this manner? Previously if 
>> display 0 was specified, it would require it to match the lsmid->slot 
>> value.  Won't it match anyway?
>>
>>> @@ -2246,6 +2282,15 @@ void security_release_secctx(struct lsmcontext 
>>> *cp)
>>>       struct security_hook_list *hp;
>>>       bool found = false;
>>> +    if (cp->slot == LSMBLOB_INVALID)
>>> +        return;
>>> +
>>> +    if (cp->slot == LSMBLOB_COMPOUND) {
>>> +        kfree(cp->context);
>>> +        found = true;
>>> +        goto clear_out;
>>> +    }
>>> +
>>
>> If you re-order your pr_warn() below with your memset() to address the 
>> earlier comment, you'll end up trying to print the freed memory.  Not 
>> a problem if you just drop the pr_warn() altogether.
> 


  reply	other threads:[~2019-12-18 20:50 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20191216223621.5127-1-casey.ref@schaufler-ca.com>
2019-12-16 22:35 ` [PATCH v12 00/25] LSM: Module stacking for AppArmor Casey Schaufler
2019-12-16 22:35   ` [PATCH v12 01/25] LSM: Infrastructure management of the sock security Casey Schaufler
2019-12-17 17:23     ` Stephen Smalley
2019-12-16 22:35   ` [PATCH v12 02/25] LSM: Create and manage the lsmblob data structure Casey Schaufler
2019-12-17 17:30     ` Stephen Smalley
2019-12-19 21:11     ` Mimi Zohar
2019-12-19 21:44       ` Casey Schaufler
2019-12-16 22:35   ` [PATCH v12 03/25] LSM: Use lsmblob in security_audit_rule_match Casey Schaufler
2019-12-17 17:34     ` Stephen Smalley
2019-12-17 22:01       ` Casey Schaufler
2019-12-17 23:47         ` Kees Cook
2019-12-18  0:28           ` Casey Schaufler
2019-12-18 13:16             ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 04/25] LSM: Use lsmblob in security_kernel_act_as Casey Schaufler
2019-12-17 17:37     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 05/25] net: Prepare UDS for security module stacking Casey Schaufler
2019-12-17 17:41     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 06/25] LSM: Use lsmblob in security_secctx_to_secid Casey Schaufler
2019-12-17 17:51     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 07/25] LSM: Use lsmblob in security_secid_to_secctx Casey Schaufler
2019-12-17 18:01     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 08/25] LSM: Use lsmblob in security_ipc_getsecid Casey Schaufler
2019-12-17 18:02     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 09/25] LSM: Use lsmblob in security_task_getsecid Casey Schaufler
2019-12-17 18:11     ` Stephen Smalley
2019-12-17 18:26       ` Casey Schaufler
2019-12-16 22:36   ` [PATCH v12 10/25] LSM: Use lsmblob in security_inode_getsecid Casey Schaufler
2019-12-17 18:13     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 11/25] LSM: Use lsmblob in security_cred_getsecid Casey Schaufler
2019-12-17 18:23     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 12/25] IMA: Change internal interfaces to use lsmblobs Casey Schaufler
2019-12-17 18:26     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 13/25] LSM: Specify which LSM to display Casey Schaufler
2019-12-18 15:17     ` Stephen Smalley
2019-12-18 16:32       ` Casey Schaufler
2019-12-16 22:36   ` [PATCH v12 14/25] LSM: Ensure the correct LSM context releaser Casey Schaufler
2019-12-18 15:53     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 15/25] LSM: Use lsmcontext in security_secid_to_secctx Casey Schaufler
2019-12-18 16:06     ` Stephen Smalley
2019-12-18 19:33     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 16/25] LSM: Use lsmcontext in security_dentry_init_security Casey Schaufler
2019-12-18 16:16     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 17/25] LSM: Use lsmcontext in security_inode_getsecctx Casey Schaufler
2019-12-18 17:02     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 18/25] LSM: security_secid_to_secctx in netlink netfilter Casey Schaufler
2019-12-18 17:10     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 19/25] NET: Store LSM netlabel data in a lsmblob Casey Schaufler
2019-12-18 17:41     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 20/25] LSM: Verify LSM display sanity in binder Casey Schaufler
2019-12-18 17:43     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 21/25] Audit: Add subj_LSM fields when necessary Casey Schaufler
2019-12-18 17:55     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 22/25] Audit: Include object data for all security modules Casey Schaufler
2019-12-18 18:02     ` Stephen Smalley
2019-12-16 22:36   ` [PATCH v12 23/25] NET: Add SO_PEERCONTEXT for multiple LSMs Casey Schaufler
2019-12-18 18:28     ` Stephen Smalley
2019-12-18 19:12       ` Stephen Smalley
2019-12-18 20:50         ` Stephen Smalley [this message]
2019-12-19 12:19           ` Simon McVittie
2019-12-19 13:47             ` Stephen Smalley
2019-12-19 15:00               ` Stephen Smalley
2019-12-19 16:48                 ` Simon McVittie
2019-12-19 17:02                   ` Stephen Smalley
2019-12-19 19:27                     ` John Johansen
2019-12-19 20:51                       ` Casey Schaufler
2019-12-19 21:41                         ` John Johansen
2019-12-19 19:21                   ` John Johansen
2019-12-16 22:36   ` [PATCH v12 24/25] LSM: Add /proc attr entry for full LSM context Casey Schaufler
2019-12-16 22:36   ` [PATCH v12 25/25] AppArmor: Remove the exclusive flag Casey Schaufler
     [not found] <20191224231339.7130-1-casey@schaufler-ca.com>
2019-12-24 23:13 ` [PATCH v12 23/25] NET: Add SO_PEERCONTEXT for multiple LSMs Casey Schaufler
2019-12-24 23:18 [PATCH v12 00/25] LSM: Module stacking for AppArmor Casey Schaufler
2019-12-24 23:19 ` [PATCH v12 23/25] NET: Add SO_PEERCONTEXT for multiple LSMs 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=a522de22-ba62-a24d-24f7-b69418e7ec0b@tycho.nsa.gov \
    --to=sds@tycho.nsa.gov \
    --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-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=selinux@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.