29 Sep, 2018

1 commit

  • commit 8c0f9f5b309d627182d5da72a69246f58bde1026 upstream.

    This changes UAPI, breaking iwd and libell:

    ell/key.c: In function 'kernel_dh_compute':
    ell/key.c:205:38: error: 'struct keyctl_dh_params' has no member named 'private'; did you mean 'dh_private'?
    struct keyctl_dh_params params = { .private = private,
    ^~~~~~~
    dh_private

    This reverts commit 8a2336e549d385bb0b46880435b411df8d8200e8.

    Fixes: 8a2336e549d3 ("uapi/linux/keyctl.h: don't use C++ reserved keyword as a struct member name")
    Signed-off-by: Lubomir Rintel
    Signed-off-by: David Howells
    cc: Randy Dunlap
    cc: Mat Martineau
    cc: Stephan Mueller
    cc: James Morris
    cc: "Serge E. Hallyn"
    cc: Mat Martineau
    cc: Andrew Morton
    cc: Linus Torvalds
    cc:
    Signed-off-by: James Morris
    Signed-off-by: Greg Kroah-Hartman

    Lubomir Rintel
     

15 Sep, 2018

1 commit

  • commit 8a2336e549d385bb0b46880435b411df8d8200e8 upstream.

    Since this header is in "include/uapi/linux/", apparently people want to
    use it in userspace programs -- even in C++ ones. However, the header
    uses a C++ reserved keyword ("private"), so change that to "dh_private"
    instead to allow the header file to be used in C++ userspace.

    Fixes https://bugzilla.kernel.org/show_bug.cgi?id=191051
    Link: http://lkml.kernel.org/r/0db6c314-1ef4-9bfa-1baa-7214dd2ee061@infradead.org
    Fixes: ddbb41148724 ("KEYS: Add KEYCTL_DH_COMPUTE command")
    Signed-off-by: Randy Dunlap
    Reviewed-by: Andrew Morton
    Cc: David Howells
    Cc: James Morris
    Cc: "Serge E. Hallyn"
    Cc: Mat Martineau
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds
    Signed-off-by: Greg Kroah-Hartman

    Randy Dunlap
     

14 Dec, 2017

2 commits

  • commit 18026d866801d0c52e5550210563222bd6c7191d upstream.

    keyctl_restrict_keyring() allows through a NULL restriction when the
    "type" is non-NULL, which causes a NULL pointer dereference in
    asymmetric_lookup_restriction() when it calls strcmp() on the
    restriction string.

    But no key types actually use a "NULL restriction" to mean anything, so
    update keyctl_restrict_keyring() to reject it with EINVAL.

    Reported-by: syzbot
    Fixes: 97d3aa0f3134 ("KEYS: Add a lookup_restriction function for the asymmetric key type")
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Signed-off-by: Greg Kroah-Hartman

    Eric Biggers
     
  • commit 4dca6ea1d9432052afb06baf2e3ae78188a4410b upstream.

    When the request_key() syscall is not passed a destination keyring, it
    links the requested key (if constructed) into the "default" request-key
    keyring. This should require Write permission to the keyring. However,
    there is actually no permission check.

    This can be abused to add keys to any keyring to which only Search
    permission is granted. This is because Search permission allows joining
    the keyring. keyctl_set_reqkey_keyring(KEY_REQKEY_DEFL_SESSION_KEYRING)
    then will set the default request-key keyring to the session keyring.
    Then, request_key() can be used to add keys to the keyring.

    Both negatively and positively instantiated keys can be added using this
    method. Adding negative keys is trivial. Adding a positive key is a
    bit trickier. It requires that either /sbin/request-key positively
    instantiates the key, or that another thread adds the key to the process
    keyring at just the right time, such that request_key() misses it
    initially but then finds it in construct_alloc_key().

    Fix this bug by checking for Write permission to the keyring in
    construct_get_dest_keyring() when the default keyring is being used.

    We don't do the permission check for non-default keyrings because that
    was already done by the earlier call to lookup_user_key(). Also,
    request_key_and_link() is currently passed a 'struct key *' rather than
    a key_ref_t, so the "possessed" bit is unavailable.

    We also don't do the permission check for the "requestor keyring", to
    continue to support the use case described by commit 8bbf4976b59f
    ("KEYS: Alter use of key instantiation link-to-keyring argument") where
    /sbin/request-key recursively calls request_key() to add keys to the
    original requestor's destination keyring. (I don't know of any users
    who actually do that, though...)

    Fixes: 3e30148c3d52 ("[PATCH] Keys: Make request-key create an authorisation key")
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Signed-off-by: Greg Kroah-Hartman

    Eric Biggers
     

03 Nov, 2017

1 commit

  • …el/git/gregkh/driver-core

    Pull initial SPDX identifiers from Greg KH:
    "License cleanup: add SPDX license identifiers to some files

    Many source files in the tree are missing licensing information, which
    makes it harder for compliance tools to determine the correct license.

    By default all files without license information are under the default
    license of the kernel, which is GPL version 2.

    Update the files which contain no license information with the
    'GPL-2.0' SPDX license identifier. The SPDX identifier is a legally
    binding shorthand, which can be used instead of the full boiler plate
    text.

    This patch is based on work done by Thomas Gleixner and Kate Stewart
    and Philippe Ombredanne.

    How this work was done:

    Patches were generated and checked against linux-4.14-rc6 for a subset
    of the use cases:

    - file had no licensing information it it.

    - file was a */uapi/* one with no licensing information in it,

    - file was a */uapi/* one with existing licensing information,

    Further patches will be generated in subsequent months to fix up cases
    where non-standard license headers were used, and references to
    license had to be inferred by heuristics based on keywords.

    The analysis to determine which SPDX License Identifier to be applied
    to a file was done in a spreadsheet of side by side results from of
    the output of two independent scanners (ScanCode & Windriver)
    producing SPDX tag:value files created by Philippe Ombredanne.
    Philippe prepared the base worksheet, and did an initial spot review
    of a few 1000 files.

    The 4.13 kernel was the starting point of the analysis with 60,537
    files assessed. Kate Stewart did a file by file comparison of the
    scanner results in the spreadsheet to determine which SPDX license
    identifier(s) to be applied to the file. She confirmed any
    determination that was not immediately clear with lawyers working with
    the Linux Foundation.

    Criteria used to select files for SPDX license identifier tagging was:

    - Files considered eligible had to be source code files.

    - Make and config files were included as candidates if they contained
    >5 lines of source

    - File already had some variant of a license header in it (even if <5
    lines).

    All documentation files were explicitly excluded.

    The following heuristics were used to determine which SPDX license
    identifiers to apply.

    - when both scanners couldn't find any license traces, file was
    considered to have no license information in it, and the top level
    COPYING file license applied.

    For non */uapi/* files that summary was:

    SPDX license identifier # files
    ---------------------------------------------------|-------
    GPL-2.0 11139

    and resulted in the first patch in this series.

    If that file was a */uapi/* path one, it was "GPL-2.0 WITH
    Linux-syscall-note" otherwise it was "GPL-2.0". Results of that
    was:

    SPDX license identifier # files
    ---------------------------------------------------|-------
    GPL-2.0 WITH Linux-syscall-note 930

    and resulted in the second patch in this series.

    - if a file had some form of licensing information in it, and was one
    of the */uapi/* ones, it was denoted with the Linux-syscall-note if
    any GPL family license was found in the file or had no licensing in
    it (per prior point). Results summary:

    SPDX license identifier # files
    ---------------------------------------------------|------
    GPL-2.0 WITH Linux-syscall-note 270
    GPL-2.0+ WITH Linux-syscall-note 169
    ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
    ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
    LGPL-2.1+ WITH Linux-syscall-note 15
    GPL-1.0+ WITH Linux-syscall-note 14
    ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
    LGPL-2.0+ WITH Linux-syscall-note 4
    LGPL-2.1 WITH Linux-syscall-note 3
    ((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
    ((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1

    and that resulted in the third patch in this series.

    - when the two scanners agreed on the detected license(s), that
    became the concluded license(s).

    - when there was disagreement between the two scanners (one detected
    a license but the other didn't, or they both detected different
    licenses) a manual inspection of the file occurred.

    - In most cases a manual inspection of the information in the file
    resulted in a clear resolution of the license that should apply
    (and which scanner probably needed to revisit its heuristics).

    - When it was not immediately clear, the license identifier was
    confirmed with lawyers working with the Linux Foundation.

    - If there was any question as to the appropriate license identifier,
    the file was flagged for further research and to be revisited later
    in time.

    In total, over 70 hours of logged manual review was done on the
    spreadsheet to determine the SPDX license identifiers to apply to the
    source files by Kate, Philippe, Thomas and, in some cases,
    confirmation by lawyers working with the Linux Foundation.

    Kate also obtained a third independent scan of the 4.13 code base from
    FOSSology, and compared selected files where the other two scanners
    disagreed against that SPDX file, to see if there was new insights.
    The Windriver scanner is based on an older version of FOSSology in
    part, so they are related.

    Thomas did random spot checks in about 500 files from the spreadsheets
    for the uapi headers and agreed with SPDX license identifier in the
    files he inspected. For the non-uapi files Thomas did random spot
    checks in about 15000 files.

    In initial set of patches against 4.14-rc6, 3 files were found to have
    copy/paste license identifier errors, and have been fixed to reflect
    the correct identifier.

    Additionally Philippe spent 10 hours this week doing a detailed manual
    inspection and review of the 12,461 patched files from the initial
    patch version early this week with:

    - a full scancode scan run, collecting the matched texts, detected
    license ids and scores

    - reviewing anything where there was a license detected (about 500+
    files) to ensure that the applied SPDX license was correct

    - reviewing anything where there was no detection but the patch
    license was not GPL-2.0 WITH Linux-syscall-note to ensure that the
    applied SPDX license was correct

    This produced a worksheet with 20 files needing minor correction. This
    worksheet was then exported into 3 different .csv files for the
    different types of files to be modified.

    These .csv files were then reviewed by Greg. Thomas wrote a script to
    parse the csv files and add the proper SPDX tag to the file, in the
    format that the file expected. This script was further refined by Greg
    based on the output to detect more types of files automatically and to
    distinguish between header and source .c files (which need different
    comment types.) Finally Greg ran the script using the .csv files to
    generate the patches.

    Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
    Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
    Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>"

    * tag 'spdx_identifiers-4.14-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core:
    License cleanup: add SPDX license identifier to uapi header files with a license
    License cleanup: add SPDX license identifier to uapi header files with no license
    License cleanup: add SPDX GPL-2.0 license identifier to files with no license

    Linus Torvalds
     

02 Nov, 2017

3 commits

  • Many source files in the tree are missing licensing information, which
    makes it harder for compliance tools to determine the correct license.

    By default all files without license information are under the default
    license of the kernel, which is GPL version 2.

    Update the files which contain no license information with the 'GPL-2.0'
    SPDX license identifier. The SPDX identifier is a legally binding
    shorthand, which can be used instead of the full boiler plate text.

    This patch is based on work done by Thomas Gleixner and Kate Stewart and
    Philippe Ombredanne.

    How this work was done:

    Patches were generated and checked against linux-4.14-rc6 for a subset of
    the use cases:
    - file had no licensing information it it.
    - file was a */uapi/* one with no licensing information in it,
    - file was a */uapi/* one with existing licensing information,

    Further patches will be generated in subsequent months to fix up cases
    where non-standard license headers were used, and references to license
    had to be inferred by heuristics based on keywords.

    The analysis to determine which SPDX License Identifier to be applied to
    a file was done in a spreadsheet of side by side results from of the
    output of two independent scanners (ScanCode & Windriver) producing SPDX
    tag:value files created by Philippe Ombredanne. Philippe prepared the
    base worksheet, and did an initial spot review of a few 1000 files.

    The 4.13 kernel was the starting point of the analysis with 60,537 files
    assessed. Kate Stewart did a file by file comparison of the scanner
    results in the spreadsheet to determine which SPDX license identifier(s)
    to be applied to the file. She confirmed any determination that was not
    immediately clear with lawyers working with the Linux Foundation.

    Criteria used to select files for SPDX license identifier tagging was:
    - Files considered eligible had to be source code files.
    - Make and config files were included as candidates if they contained >5
    lines of source
    - File already had some variant of a license header in it (even if
    Reviewed-by: Philippe Ombredanne
    Reviewed-by: Thomas Gleixner
    Signed-off-by: Greg Kroah-Hartman

    Greg Kroah-Hartman
     
  • When calling keyctl_read() on a key of type "trusted", if the
    user-supplied buffer was too small, the kernel ignored the buffer length
    and just wrote past the end of the buffer, potentially corrupting
    userspace memory. Fix it by instead returning the size required, as per
    the documentation for keyctl_read().

    We also don't even fill the buffer at all in this case, as this is
    slightly easier to implement than doing a short read, and either
    behavior appears to be permitted. It also makes it match the behavior
    of the "encrypted" key type.

    Fixes: d00a1c72f7f4 ("keys: add new trusted key-type")
    Reported-by: Ben Hutchings
    Cc: # v2.6.38+
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Reviewed-by: Mimi Zohar
    Reviewed-by: James Morris
    Signed-off-by: James Morris

    Eric Biggers
     
  • Commit e645016abc80 ("KEYS: fix writing past end of user-supplied buffer
    in keyring_read()") made keyring_read() stop corrupting userspace memory
    when the user-supplied buffer is too small. However it also made the
    return value in that case be the short buffer size rather than the size
    required, yet keyctl_read() is actually documented to return the size
    required. Therefore, switch it over to the documented behavior.

    Note that for now we continue to have it fill the short buffer, since it
    did that before (pre-v3.13) and dump_key_tree_aux() in keyutils arguably
    relies on it.

    Fixes: e645016abc80 ("KEYS: fix writing past end of user-supplied buffer in keyring_read()")
    Reported-by: Ben Hutchings
    Cc: # v3.13+
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Reviewed-by: James Morris
    Signed-off-by: James Morris

    Eric Biggers
     

18 Oct, 2017

6 commits

  • In proc_keys_show(), the key semaphore is not held, so the key ->flags
    and ->expiry can be changed concurrently. We therefore should read them
    atomically just once.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • Similar to the case for key_validate(), we should load the key ->expiry
    once atomically in keyring_search_iterator(), since it can be changed
    concurrently with the flags whenever the key semaphore isn't held.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • In key_validate(), load the flags and expiry time once atomically, since
    these can change concurrently if key_validate() is called without the
    key semaphore held. And we don't want to get inconsistent results if a
    variable is referenced multiple times. For example, key->expiry was
    referenced in both 'if (key->expiry)' and in 'if (now.tv_sec >=
    key->expiry)', making it theoretically possible to see a spurious
    EKEYEXPIRED while the expiration time was being removed, i.e. set to 0.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • Currently, when passed a key that already exists, add_key() will call the
    key's ->update() method if such exists. But this is heavily broken in the
    case where the key is uninstantiated because it doesn't call
    __key_instantiate_and_link(). Consequently, it doesn't do most of the
    things that are supposed to happen when the key is instantiated, such as
    setting the instantiation state, clearing KEY_FLAG_USER_CONSTRUCT and
    awakening tasks waiting on it, and incrementing key->user->nikeys.

    It also never takes key_construction_mutex, which means that
    ->instantiate() can run concurrently with ->update() on the same key. In
    the case of the "user" and "logon" key types this causes a memory leak, at
    best. Maybe even worse, the ->update() methods of the "encrypted" and
    "trusted" key types actually just dereference a NULL pointer when passed an
    uninstantiated key.

    Change key_create_or_update() to wait interruptibly for the key to finish
    construction before continuing.

    This patch only affects *uninstantiated* keys. For now we still allow a
    negatively instantiated key to be updated (thereby positively
    instantiating it), although that's broken too (the next patch fixes it)
    and I'm not sure that anyone actually uses that functionality either.

    Here is a simple reproducer for the bug using the "encrypted" key type
    (requires CONFIG_ENCRYPTED_KEYS=y), though as noted above the bug
    pertained to more than just the "encrypted" key type:

    #include
    #include
    #include

    int main(void)
    {
    int ringid = keyctl_join_session_keyring(NULL);

    if (fork()) {
    for (;;) {
    const char payload[] = "update user:foo 32";

    usleep(rand() % 10000);
    add_key("encrypted", "desc", payload, sizeof(payload), ringid);
    keyctl_clear(ringid);
    }
    } else {
    for (;;)
    request_key("encrypted", "desc", "callout_info", ringid);
    }
    }

    It causes:

    BUG: unable to handle kernel NULL pointer dereference at 0000000000000018
    IP: encrypted_update+0xb0/0x170
    PGD 7a178067 P4D 7a178067 PUD 77269067 PMD 0
    PREEMPT SMP
    CPU: 0 PID: 340 Comm: reproduce Tainted: G D 4.14.0-rc1-00025-g428490e38b2e #796
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
    task: ffff8a467a39a340 task.stack: ffffb15c40770000
    RIP: 0010:encrypted_update+0xb0/0x170
    RSP: 0018:ffffb15c40773de8 EFLAGS: 00010246
    RAX: 0000000000000000 RBX: ffff8a467a275b00 RCX: 0000000000000000
    RDX: 0000000000000005 RSI: ffff8a467a275b14 RDI: ffffffffb742f303
    RBP: ffffb15c40773e20 R08: 0000000000000000 R09: ffff8a467a275b17
    R10: 0000000000000020 R11: 0000000000000000 R12: 0000000000000000
    R13: 0000000000000000 R14: ffff8a4677057180 R15: ffff8a467a275b0f
    FS: 00007f5d7fb08700(0000) GS:ffff8a467f200000(0000) knlGS:0000000000000000
    CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    CR2: 0000000000000018 CR3: 0000000077262005 CR4: 00000000001606f0
    Call Trace:
    key_create_or_update+0x2bc/0x460
    SyS_add_key+0x10c/0x1d0
    entry_SYSCALL_64_fastpath+0x1f/0xbe
    RIP: 0033:0x7f5d7f211259
    RSP: 002b:00007ffed03904c8 EFLAGS: 00000246 ORIG_RAX: 00000000000000f8
    RAX: ffffffffffffffda RBX: 000000003b2a7955 RCX: 00007f5d7f211259
    RDX: 00000000004009e4 RSI: 00000000004009ff RDI: 0000000000400a04
    RBP: 0000000068db8bad R08: 000000003b2a7955 R09: 0000000000000004
    R10: 000000000000001a R11: 0000000000000246 R12: 0000000000400868
    R13: 00007ffed03905d0 R14: 0000000000000000 R15: 0000000000000000
    Code: 77 28 e8 64 34 1f 00 45 31 c0 31 c9 48 8d 55 c8 48 89 df 48 8d 75 d0 e8 ff f9 ff ff 85 c0 41 89 c4 0f 88 84 00 00 00 4c 8b 7d c8 8b 75 18 4c 89 ff e8 24 f8 ff ff 85 c0 41 89 c4 78 6d 49 8b
    RIP: encrypted_update+0xb0/0x170 RSP: ffffb15c40773de8
    CR2: 0000000000000018

    Cc: # v2.6.12+
    Reported-by: Eric Biggers
    Signed-off-by: David Howells
    cc: Eric Biggers

    David Howells
     
  • Consolidate KEY_FLAG_INSTANTIATED, KEY_FLAG_NEGATIVE and the rejection
    error into one field such that:

    (1) The instantiation state can be modified/read atomically.

    (2) The error can be accessed atomically with the state.

    (3) The error isn't stored unioned with the payload pointers.

    This deals with the problem that the state is spread over three different
    objects (two bits and a separate variable) and reading or updating them
    atomically isn't practical, given that not only can uninstantiated keys
    change into instantiated or rejected keys, but rejected keys can also turn
    into instantiated keys - and someone accessing the key might not be using
    any locking.

    The main side effect of this problem is that what was held in the payload
    may change, depending on the state. For instance, you might observe the
    key to be in the rejected state. You then read the cached error, but if
    the key semaphore wasn't locked, the key might've become instantiated
    between the two reads - and you might now have something in hand that isn't
    actually an error code.

    The state is now KEY_IS_UNINSTANTIATED, KEY_IS_POSITIVE or a negative error
    code if the key is negatively instantiated. The key_is_instantiated()
    function is replaced with key_is_positive() to avoid confusion as negative
    keys are also 'instantiated'.

    Additionally, barriering is included:

    (1) Order payload-set before state-set during instantiation.

    (2) Order state-read before payload-read when using the key.

    Further separate barriering is necessary if RCU is being used to access the
    payload content after reading the payload pointers.

    Fixes: 146aa8b1453b ("KEYS: Merge the type-specific data with the payload data")
    Cc: stable@vger.kernel.org # v4.4+
    Reported-by: Eric Biggers
    Signed-off-by: David Howells
    Reviewed-by: Eric Biggers

    David Howells
     
  • The recent rework introduced a possible randconfig build failure
    when CONFIG_CRYPTO configured to only allow modules:

    security/keys/big_key.o: In function `big_key_crypt':
    big_key.c:(.text+0x29f): undefined reference to `crypto_aead_setkey'
    security/keys/big_key.o: In function `big_key_init':
    big_key.c:(.init.text+0x1a): undefined reference to `crypto_alloc_aead'
    big_key.c:(.init.text+0x45): undefined reference to `crypto_aead_setauthsize'
    big_key.c:(.init.text+0x77): undefined reference to `crypto_destroy_tfm'
    crypto/gcm.o: In function `gcm_hash_crypt_remain_continue':
    gcm.c:(.text+0x167): undefined reference to `crypto_ahash_finup'
    crypto/gcm.o: In function `crypto_gcm_exit_tfm':
    gcm.c:(.text+0x847): undefined reference to `crypto_destroy_tfm'

    When we 'select CRYPTO' like the other users, we always get a
    configuration that builds.

    Fixes: 428490e38b2e ("security/keys: rewrite all of big_key crypto")
    Signed-off-by: Arnd Bergmann
    Signed-off-by: David Howells

    Arnd Bergmann
     

12 Oct, 2017

1 commit

  • A key of type "encrypted" references a "master key" which is used to
    encrypt and decrypt the encrypted key's payload. However, when we
    accessed the master key's payload, we failed to handle the case where
    the master key has been revoked, which sets the payload pointer to NULL.
    Note that request_key() *does* skip revoked keys, but there is still a
    window where the key can be revoked before we acquire its semaphore.

    Fix it by checking for a NULL payload, treating it like a key which was
    already revoked at the time it was requested.

    This was an issue for master keys of type "user" only. Master keys can
    also be of type "trusted", but those cannot be revoked.

    Fixes: 7e70cb497850 ("keys: add new key-type encrypted")
    Reviewed-by: James Morris
    Cc: [v2.6.38+]
    Cc: Mimi Zohar
    Cc: David Safford
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     

26 Sep, 2017

2 commits

  • This started out as just replacing the use of crypto/rng with
    get_random_bytes_wait, so that we wouldn't use bad randomness at boot
    time. But, upon looking further, it appears that there were even deeper
    underlying cryptographic problems, and that this seems to have been
    committed with very little crypto review. So, I rewrote the whole thing,
    trying to keep to the conventions introduced by the previous author, to
    fix these cryptographic flaws.

    It makes no sense to seed crypto/rng at boot time and then keep
    using it like this, when in fact there's already get_random_bytes_wait,
    which can ensure there's enough entropy and be a much more standard way
    of generating keys. Since this sensitive material is being stored
    untrusted, using ECB and no authentication is simply not okay at all. I
    find it surprising and a bit horrifying that this code even made it past
    basic crypto review, which perhaps points to some larger issues. This
    patch moves from using AES-ECB to using AES-GCM. Since keys are uniquely
    generated each time, we can set the nonce to zero. There was also a race
    condition in which the same key would be reused at the same time in
    different threads. A mutex fixes this issue now.

    So, to summarize, this commit fixes the following vulnerabilities:

    * Low entropy key generation, allowing an attacker to potentially
    guess or predict keys.
    * Unauthenticated encryption, allowing an attacker to modify the
    cipher text in particular ways in order to manipulate the plaintext,
    which is is even more frightening considering the next point.
    * Use of ECB mode, allowing an attacker to trivially swap blocks or
    compare identical plaintext blocks.
    * Key re-use.
    * Faulty memory zeroing.

    Signed-off-by: Jason A. Donenfeld
    Reviewed-by: Eric Biggers
    Signed-off-by: David Howells
    Cc: Herbert Xu
    Cc: Kirill Marinushkin
    Cc: security@kernel.org
    Cc: stable@vger.kernel.org

    Jason A. Donenfeld
     
  • Error paths forgot to zero out sensitive material, so this patch changes
    some kfrees into a kzfrees.

    Signed-off-by: Jason A. Donenfeld
    Signed-off-by: David Howells
    Reviewed-by: Eric Biggers
    Cc: Herbert Xu
    Cc: Kirill Marinushkin
    Cc: security@kernel.org
    Cc: stable@vger.kernel.org

    Jason A. Donenfeld
     

25 Sep, 2017

10 commits

  • kmemdup() is preferred to kmalloc() followed by memcpy().

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • When checking for permission to view keys whilst reading from
    /proc/keys, we should use the credentials with which the /proc/keys file
    was opened. This is because, in a classic type of exploit, it can be
    possible to bypass checks for the *current* credentials by passing the
    file descriptor to a suid program.

    Following commit 34dbbcdbf633 ("Make file credentials available to the
    seqfile interfaces") we can finally fix it. So let's do it.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • In key_user_lookup(), if there is no key_user for the given uid, we drop
    key_user_lock, allocate a new key_user, and search the tree again. But
    we failed to set 'parent' to NULL at the beginning of the second search.
    If the tree were to be empty for the second search, the insertion would
    be done with an invalid 'parent', scribbling over freed memory.

    Fortunately this can't actually happen currently because the tree always
    contains at least the root_key_user. But it still should be fixed to
    make the code more robust.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • Because keyctl_read_key() looks up the key with no permissions
    requested, it may find a negatively instantiated key. If the key is
    also possessed, we went ahead and called ->read() on the key. But the
    key payload will actually contain the ->reject_error rather than the
    normal payload. Thus, the kernel oopses trying to read the
    user_key_payload from memory address (int)-ENOKEY = 0x00000000ffffff82.

    Fortunately the payload data is stored inline, so it shouldn't be
    possible to abuse this as an arbitrary memory read primitive...

    Reproducer:
    keyctl new_session
    keyctl request2 user desc '' @s
    keyctl read $(keyctl show | awk '/user: desc/ {print $1}')

    It causes a crash like the following:
    BUG: unable to handle kernel paging request at 00000000ffffff92
    IP: user_read+0x33/0xa0
    PGD 36a54067 P4D 36a54067 PUD 0
    Oops: 0000 [#1] SMP
    CPU: 0 PID: 211 Comm: keyctl Not tainted 4.14.0-rc1 #337
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-20170228_101828-anatol 04/01/2014
    task: ffff90aa3b74c3c0 task.stack: ffff9878c0478000
    RIP: 0010:user_read+0x33/0xa0
    RSP: 0018:ffff9878c047bee8 EFLAGS: 00010246
    RAX: 0000000000000001 RBX: ffff90aa3d7da340 RCX: 0000000000000017
    RDX: 0000000000000000 RSI: 00000000ffffff82 RDI: ffff90aa3d7da340
    RBP: ffff9878c047bf00 R08: 00000024f95da94f R09: 0000000000000000
    R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000000
    R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
    FS: 00007f58ece69740(0000) GS:ffff90aa3e200000(0000) knlGS:0000000000000000
    CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    CR2: 00000000ffffff92 CR3: 0000000036adc001 CR4: 00000000003606f0
    Call Trace:
    keyctl_read_key+0xac/0xe0
    SyS_keyctl+0x99/0x120
    entry_SYSCALL_64_fastpath+0x1f/0xbe
    RIP: 0033:0x7f58ec787bb9
    RSP: 002b:00007ffc8d401678 EFLAGS: 00000206 ORIG_RAX: 00000000000000fa
    RAX: ffffffffffffffda RBX: 00007ffc8d402800 RCX: 00007f58ec787bb9
    RDX: 0000000000000000 RSI: 00000000174a63ac RDI: 000000000000000b
    RBP: 0000000000000004 R08: 00007ffc8d402809 R09: 0000000000000020
    R10: 0000000000000000 R11: 0000000000000206 R12: 00007ffc8d402800
    R13: 00007ffc8d4016e0 R14: 0000000000000000 R15: 0000000000000000
    Code: e5 41 55 49 89 f5 41 54 49 89 d4 53 48 89 fb e8 a4 b4 ad ff 85 c0 74 09 80 3d b9 4c 96 00 00 74 43 48 8b b3 20 01 00 00 4d 85 ed b7 5e 10 74 29 4d 85 e4 74 24 4c 39 e3 4c 89 e2 4c 89 ef 48
    RIP: user_read+0x33/0xa0 RSP: ffff9878c047bee8
    CR2: 00000000ffffff92

    Fixes: 61ea0c0ba904 ("KEYS: Skip key state checks when checking for possession")
    Cc: [v3.13+]
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • It was possible for an unprivileged user to create the user and user
    session keyrings for another user. For example:

    sudo -u '#3000' sh -c 'keyctl add keyring _uid.4000 "" @u
    keyctl add keyring _uid_ses.4000 "" @u
    sleep 15' &
    sleep 1
    sudo -u '#4000' keyctl describe @u
    sudo -u '#4000' keyctl describe @us

    This is problematic because these "fake" keyrings won't have the right
    permissions. In particular, the user who created them first will own
    them and will have full access to them via the possessor permissions,
    which can be used to compromise the security of a user's keys:

    -4: alswrv-----v------------ 3000 0 keyring: _uid.4000
    -5: alswrv-----v------------ 3000 0 keyring: _uid_ses.4000

    Fix it by marking user and user session keyrings with a flag
    KEY_FLAG_UID_KEYRING. Then, when searching for a user or user session
    keyring by name, skip all keyrings that don't have the flag set.

    Fixes: 69664cf16af4 ("keys: don't generate user and user session keyrings unless they're accessed")
    Cc: [v2.6.26+]
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • Userspace can call keyctl_read() on a keyring to get the list of IDs of
    keys in the keyring. But if the user-supplied buffer is too small, the
    kernel would write the full list anyway --- which will corrupt whatever
    userspace memory happened to be past the end of the buffer. Fix it by
    only filling the space that is available.

    Fixes: b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
    Cc: [v3.13+]
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • In keyctl_read_key(), if key_permission() were to return an error code
    other than EACCES, we would leak a the reference to the key. This can't
    actually happen currently because key_permission() can only return an
    error code other than EACCES if security_key_permission() does, only
    SELinux and Smack implement that hook, and neither can return an error
    code other than EACCES. But it should still be fixed, as it is a bug
    waiting to happen.

    Fixes: 29db91906340 ("[PATCH] Keys: Add LSM hooks for key management [try #3]")
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • In keyctl_assume_authority(), if keyctl_change_reqkey_auth() were to
    fail, we would leak the reference to the 'authkey'. Currently this can
    only happen if prepare_creds() fails to allocate memory. But it still
    should be fixed, as it is a more severe bug waiting to happen.

    This patch also moves the read of 'authkey->serial' to before the
    reference to the authkey is dropped. Doing the read after dropping the
    reference is very fragile because it assumes we still hold another
    reference to the key. (Which we do, in current->cred->request_key_auth,
    but there's no reason not to write it in the "obviously correct" way.)

    Fixes: d84f4f992cbd ("CRED: Inaugurate COW credentials")
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • If key_instantiate_and_link() were to fail (which fortunately isn't
    possible currently), the call to key_revoke(authkey) would crash with a
    NULL pointer dereference in request_key_auth_revoke() because the key
    has not yet been instantiated.

    Fix this by removing the call to key_revoke(). key_put() is sufficient,
    as it's not possible for an uninstantiated authkey to have been used for
    anything yet.

    Fixes: b5f545c880a2 ("[PATCH] keys: Permit running process to instantiate keys")
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     
  • In request_key_auth_new(), if key_alloc() or key_instantiate_and_link()
    were to fail, we would leak a reference to the 'struct cred'. Currently
    this can only happen if key_alloc() fails to allocate memory. But it
    still should be fixed, as it is a more severe bug waiting to happen.

    Fix it by cleaning things up to use a helper function which frees a
    'struct request_key_auth' correctly.

    Fixes: d84f4f992cbd ("CRED: Inaugurate COW credentials")
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells

    Eric Biggers
     

05 Sep, 2017

2 commits

  • Make the position an in/out argument like all the other read/write
    helpers and and make the buf argument a void pointer.

    Signed-off-by: Christoph Hellwig
    Signed-off-by: Al Viro

    Christoph Hellwig
     
  • Use proper ssize_t and size_t types for the return value and count
    argument, move the offset last and make it an in/out argument like
    all other read/write helpers, and make the buf argument a void pointer
    to get rid of lots of casts in the callers.

    Signed-off-by: Christoph Hellwig
    Signed-off-by: Al Viro

    Christoph Hellwig
     

19 Jul, 2017

1 commit

  • Pull structure randomization updates from Kees Cook:
    "Now that IPC and other changes have landed, enable manual markings for
    randstruct plugin, including the task_struct.

    This is the rest of what was staged in -next for the gcc-plugins, and
    comes in three patches, largest first:

    - mark "easy" structs with __randomize_layout

    - mark task_struct with an optional anonymous struct to isolate the
    __randomize_layout section

    - mark structs to opt _out_ of automated marking (which will come
    later)

    And, FWIW, this continues to pass allmodconfig (normal and patched to
    enable gcc-plugins) builds of x86_64, i386, arm64, arm, powerpc, and
    s390 for me"

    * tag 'gcc-plugins-v4.13-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
    randstruct: opt-out externally exposed function pointer structs
    task_struct: Allow randomized layout
    randstruct: Mark various structs for randomization

    Linus Torvalds
     

14 Jul, 2017

1 commit

  • Syscalls must validate that their reserved arguments are zero and return
    EINVAL otherwise. Otherwise, it will be impossible to actually use them
    for anything in the future because existing programs may be passing
    garbage in. This is standard practice when adding new APIs.

    Cc: stable@vger.kernel.org
    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Signed-off-by: James Morris

    Eric Biggers
     

04 Jul, 2017

1 commit

  • Pull documentation updates from Jonathan Corbet:
    "There has been a fair amount of activity in the docs tree this time
    around. Highlights include:

    - Conversion of a bunch of security documentation into RST

    - The conversion of the remaining DocBook templates by The Amazing
    Mauro Machine. We can now drop the entire DocBook build chain.

    - The usual collection of fixes and minor updates"

    * tag 'docs-4.13' of git://git.lwn.net/linux: (90 commits)
    scripts/kernel-doc: handle DECLARE_HASHTABLE
    Documentation: atomic_ops.txt is core-api/atomic_ops.rst
    Docs: clean up some DocBook loose ends
    Make the main documentation title less Geocities
    Docs: Use kernel-figure in vidioc-g-selection.rst
    Docs: fix table problems in ras.rst
    Docs: Fix breakage with Sphinx 1.5 and upper
    Docs: Include the Latex "ifthen" package
    doc/kokr/howto: Only send regression fixes after -rc1
    docs-rst: fix broken links to dynamic-debug-howto in kernel-parameters
    doc: Document suitability of IBM Verse for kernel development
    Doc: fix a markup error in coding-style.rst
    docs: driver-api: i2c: remove some outdated information
    Documentation: DMA API: fix a typo in a function name
    Docs: Insert missing space to separate link from text
    doc/ko_KR/memory-barriers: Update control-dependencies example
    Documentation, kbuild: fix typo "minimun" -> "minimum"
    docs: Fix some formatting issues in request-key.rst
    doc: ReSTify keys-trusted-encrypted.txt
    doc: ReSTify keys-request-key.txt
    ...

    Linus Torvalds
     

01 Jul, 2017

1 commit

  • This marks many critical kernel structures for randomization. These are
    structures that have been targeted in the past in security exploits, or
    contain functions pointers, pointers to function pointer tables, lists,
    workqueues, ref-counters, credentials, permissions, or are otherwise
    sensitive. This initial list was extracted from Brad Spengler/PaX Team's
    code in the last public patch of grsecurity/PaX based on my understanding
    of the code. Changes or omissions from the original code are mine and
    don't reflect the original grsecurity/PaX code.

    Left out of this list is task_struct, which requires special handling
    and will be covered in a subsequent patch.

    Signed-off-by: Kees Cook

    Kees Cook
     

20 Jun, 2017

1 commit

  • The wait_bit*() types and APIs are mixed into wait.h, but they
    are a pretty orthogonal extension of wait-queues.

    Furthermore, only about 50 kernel files use these APIs, while
    over 1000 use the regular wait-queue functionality.

    So clean up the main wait.h by moving the wait-bit functionality
    out of it, into a separate .h and .c file:

    include/linux/wait_bit.h for types and APIs
    kernel/sched/wait_bit.c for the implementation

    Update all header dependencies.

    This reduces the size of wait.h rather significantly, by about 30%.

    Cc: Linus Torvalds
    Cc: Peter Zijlstra
    Cc: Thomas Gleixner
    Cc: linux-kernel@vger.kernel.org
    Signed-off-by: Ingo Molnar

    Ingo Molnar
     

09 Jun, 2017

6 commits

  • If a key's refcount is dropped to zero between key_lookup() peeking at
    the refcount and subsequently attempting to increment it, refcount_inc()
    will see a zero refcount. Here, refcount_inc() will WARN_ONCE(), and
    will *not* increment the refcount, which will remain zero.

    Once key_lookup() drops key_serial_lock, it is possible for the key to
    be freed behind our back.

    This patch uses refcount_inc_not_zero() to perform the peek and increment
    atomically.

    Fixes: fff292914d3a2f1e ("security, keys: convert key.usage from atomic_t to refcount_t")
    Signed-off-by: Mark Rutland
    Signed-off-by: David Howells
    Cc: David Windsor
    Cc: Elena Reshetova
    Cc: Hans Liljestrand
    Cc: James Morris
    Cc: Kees Cook
    Cc: Peter Zijlstra
    Signed-off-by: James Morris

    Mark Rutland
     
  • The initial Diffie-Hellman computation made direct use of the MPI
    library because the crypto module did not support DH at the time. Now
    that KPP is implemented, KEYCTL_DH_COMPUTE should use it to get rid of
    duplicate code and leverage possible hardware acceleration.

    This fixes an issue whereby the input to the KDF computation would
    include additional uninitialized memory when the result of the
    Diffie-Hellman computation was shorter than the input prime number.

    Signed-off-by: Mat Martineau
    Signed-off-by: David Howells
    Signed-off-by: James Morris

    Mat Martineau
     
  • Accessing a 'u8[4]' through a '__be32 *' violates alignment rules. Just
    make the counter a __be32 instead.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Acked-by: Stephan Mueller
    Signed-off-by: James Morris

    Eric Biggers
     
  • If userspace called KEYCTL_DH_COMPUTE with kdf_params containing NULL
    otherinfo but nonzero otherinfolen, the kernel would allocate a buffer
    for the otherinfo, then feed it into the KDF without initializing it.
    Fix this by always doing the copy from userspace (which will fail with
    EFAULT in this scenario).

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Acked-by: Stephan Mueller
    Signed-off-by: James Morris

    Eric Biggers
     
  • Requesting "digest_null" in the keyctl_kdf_params caused an infinite
    loop in kdf_ctr() because the "null" hash has a digest size of 0. Fix
    it by rejecting hash algorithms with a digest size of 0.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Acked-by: Stephan Mueller
    Signed-off-by: James Morris

    Eric Biggers
     
  • While a 'struct key' itself normally does not contain sensitive
    information, Documentation/security/keys.txt actually encourages this:

    "Having a payload is not required; and the payload can, in fact,
    just be a value stored in the struct key itself."

    In case someone has taken this advice, or will take this advice in the
    future, zero the key structure before freeing it. We might as well, and
    as a bonus this could make it a bit more difficult for an adversary to
    determine which keys have recently been in use.

    This is safe because the key_jar cache does not use a constructor.

    Signed-off-by: Eric Biggers
    Signed-off-by: David Howells
    Signed-off-by: James Morris

    Eric Biggers