1. 05 Dec, 2017 1 commit
    • John Johansen's avatar
      apparmor: fix oops in audit_signal_cb hook · 69af2269
      John Johansen authored
      commit b12cbb21586277f72533769832c24cc6c1d60ab3 upstream.
      
      The apparmor_audit_data struct ordering got messed up during a merge
      conflict, resulting in the signal integer and peer pointer being in
      a union instead of a struct.
      
      For most of the 4.13 and 4.14 life cycle, this was hidden by
      commit 651e28c5 ("apparmor: add base infastructure for socket
      mediation") which fixed the apparmor_audit_data struct when its data
      was added. When that commit was reverted in -rc7 the signal audit bug
      was exposed, and unfortunately it never showed up in any of the
      testing until after 4.14 was released. Shaun Khan, Zephaniah
      E. Loss-Cutler-Hull filed nearly simultaneous bug reports (with
      different oopes, the smaller of which is included below).
      
      Full credit goes to Tetsuo Handa for jumping on this as well and
      noticing the audit data struct problem and reporting it.
      
      [   76.178568] BUG: unable to handle kernel paging request at
      ffffffff0eee3bc0
      [   76.178579] IP: audit_signal_cb+0x6c/0xe0
      [   76.178581] PGD 1a640a067 P4D 1a640a067 PUD 0
      [   76.178586] Oops: 0000 [#1] PREEMPT SMP
      [   76.178589] Modules linked in: fuse rfcomm bnep usblp uvcvideo btusb
      btrtl btbcm btintel bluetooth ecdh_generic ip6table_filter ip6_tables
      xt_tcpudp nf_conntrack_ipv4 nf_defrag_ipv4 xt_conntrack nf_conntrack
      iptable_filter ip_tables x_tables intel_rapl joydev wmi_bmof serio_raw
      iwldvm iwlwifi shpchp kvm_intel kvm irqbypass autofs4 algif_skcipher
      nls_iso8859_1 nls_cp437 crc32_pclmul ghash_clmulni_intel
      [   76.178620] CPU: 0 PID: 10675 Comm: pidgin Not tainted
      4.14.0-f1-dirty #135
      [   76.178623] Hardware name: Hewlett-Packard HP EliteBook Folio
      9470m/18DF, BIOS 68IBD Ver. F.62 10/22/2015
      [   76.178625] task: ffff9c7a94c31dc0 task.stack: ffffa09b02a4c000
      [   76.178628] RIP: 0010:audit_signal_cb+0x6c/0xe0
      [   76.178631] RSP: 0018:ffffa09b02a4fc08 EFLAGS: 00010292
      [   76.178634] RAX: ffffa09b02a4fd60 RBX: ffff9c7aee0741f8 RCX:
      0000000000000000
      [   76.178636] RDX: ffffffffee012290 RSI: 0000000000000006 RDI:
      ffff9c7a9493d800
      [   76.178638] RBP: ffffa09b02a4fd40 R08: 000000000000004d R09:
      ffffa09b02a4fc46
      [   76.178641] R10: ffffa09b02a4fcb8 R11: ffff9c7ab44f5072 R12:
      ffffa09b02a4fd40
      [   76.178643] R13: ffffffff9e447be0 R14: ffff9c7a94c31dc0 R15:
      0000000000000001
      [   76.178646] FS:  00007f8b11ba2a80(0000) GS:ffff9c7afea00000(0000)
      knlGS:0000000000000000
      [   76.178648] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
      [   76.178650] CR2: ffffffff0eee3bc0 CR3: 00000003d5209002 CR4:
      00000000001606f0
      [   76.178652] Call Trace:
      [   76.178660]  common_lsm_audit+0x1da/0x780
      [   76.178665]  ? d_absolute_path+0x60/0x90
      [   76.178669]  ? aa_check_perms+0xcd/0xe0
      [   76.178672]  aa_check_perms+0xcd/0xe0
      [   76.178675]  profile_signal_perm.part.0+0x90/0xa0
      [   76.178679]  aa_may_signal+0x16e/0x1b0
      [   76.178686]  apparmor_task_kill+0x51/0x120
      [   76.178690]  security_task_kill+0x44/0x60
      [   76.178695]  group_send_sig_info+0x25/0x60
      [   76.178699]  kill_pid_info+0x36/0x60
      [   76.178703]  SYSC_kill+0xdb/0x180
      [   76.178707]  ? preempt_count_sub+0x92/0xd0
      [   76.178712]  ? _raw_write_unlock_irq+0x13/0x30
      [   76.178716]  ? task_work_run+0x6a/0x90
      [   76.178720]  ? exit_to_usermode_loop+0x80/0xa0
      [   76.178723]  entry_SYSCALL_64_fastpath+0x13/0x94
      [   76.178727] RIP: 0033:0x7f8b0e58b767
      [   76.178729] RSP: 002b:00007fff19efd4d8 EFLAGS: 00000206 ORIG_RAX:
      000000000000003e
      [   76.178732] RAX: ffffffffffffffda RBX: 0000557f3e3c2050 RCX:
      00007f8b0e58b767
      [   76.178735] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
      000000000000263b
      [   76.178737] RBP: 0000000000000000 R08: 0000557f3e3c2270 R09:
      0000000000000001
      [   76.178739] R10: 000000000000022d R11: 0000000000000206 R12:
      0000000000000000
      [   76.178741] R13: 0000000000000001 R14: 0000557f3e3c13c0 R15:
      0000000000000000
      [   76.178745] Code: 48 8b 55 18 48 89 df 41 b8 20 00 08 01 5b 5d 48 8b
      42 10 48 8b 52 30 48 63 48 4c 48 8b 44 c8 48 31 c9 48 8b 70 38 e9 f4 fd
      00 00 <48> 8b 14 d5 40 27 e5 9e 48 c7 c6 7d 07 19 9f 48 89 df e8 fd 35
      [   76.178794] RIP: audit_signal_cb+0x6c/0xe0 RSP: ffffa09b02a4fc08
      [   76.178796] CR2: ffffffff0eee3bc0
      [   76.178799] ---[ end trace 514af9529297f1a3 ]---
      
      Fixes: cd1dbf76 ("apparmor: add the ability to mediate signals")
      Reported-by: 's avatarZephaniah E. Loss-Cutler-Hull <warp-spam_kernel@aehallh.com>
      Reported-by: 's avatarShuah Khan <shuahkh@osg.samsung.com>
      Suggested-by: 's avatarTetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
      Tested-by: 's avatarIvan Kozik <ivan@ludios.org>
      Tested-by: 's avatarZephaniah E. Loss-Cutler-Hull <warp-spam_kernel@aehallh.com>
      Tested-by: 's avatarChristian Boltz <apparmor@cboltz.de>
      Tested-by: 's avatarShuah Khan <shuahkh@osg.samsung.com>
      Signed-off-by: 's avatarJohn Johansen <john.johansen@canonical.com>
      Signed-off-by: 's avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      69af2269
  2. 24 Nov, 2017 1 commit
  3. 08 Nov, 2017 1 commit
  4. 02 Nov, 2017 3 commits
    • Greg Kroah-Hartman's avatar
      License cleanup: add SPDX GPL-2.0 license identifier to files with no license · b2441318
      Greg Kroah-Hartman authored
      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: 's avatarKate Stewart <kstewart@linuxfoundation.org>
      Reviewed-by: 's avatarPhilippe Ombredanne <pombredanne@nexb.com>
      Reviewed-by: 's avatarThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: 's avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      b2441318
    • Eric Biggers's avatar
      KEYS: trusted: fix writing past end of buffer in trusted_read() · a3c812f7
      Eric Biggers authored
      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: d00a1c72 ("keys: add new trusted key-type")
      Reported-by: 's avatarBen Hutchings <ben@decadent.org.uk>
      Cc: <stable@vger.kernel.org> # v2.6.38+
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      Reviewed-by: 's avatarMimi Zohar <zohar@linux.vnet.ibm.com>
      Reviewed-by: 's avatarJames Morris <james.l.morris@oracle.com>
      Signed-off-by: 's avatarJames Morris <james.l.morris@oracle.com>
      a3c812f7
    • Eric Biggers's avatar
      KEYS: return full count in keyring_read() if buffer is too small · 3239b6f2
      Eric Biggers authored
      Commit e645016a ("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: e645016a ("KEYS: fix writing past end of user-supplied buffer in keyring_read()")
      Reported-by: 's avatarBen Hutchings <ben@decadent.org.uk>
      Cc: <stable@vger.kernel.org> # v3.13+
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      Reviewed-by: 's avatarJames Morris <james.l.morris@oracle.com>
      Signed-off-by: 's avatarJames Morris <james.l.morris@oracle.com>
      3239b6f2
  5. 26 Oct, 2017 1 commit
  6. 19 Oct, 2017 1 commit
  7. 18 Oct, 2017 6 commits
    • Eric Biggers's avatar
      KEYS: load key flags and expiry time atomically in proc_keys_show() · ab5c69f0
      Eric Biggers authored
      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: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      ab5c69f0
    • Eric Biggers's avatar
      KEYS: Load key expiry time atomically in keyring_search_iterator() · 9d6c8711
      Eric Biggers authored
      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: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      9d6c8711
    • Eric Biggers's avatar
      KEYS: load key flags and expiry time atomically in key_validate() · 1823d475
      Eric Biggers authored
      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: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      1823d475
    • David Howells's avatar
      KEYS: don't let add_key() update an uninstantiated key · 60ff5b2f
      David Howells authored
      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 <stdlib.h>
          #include <unistd.h>
          #include <keyutils.h>
      
          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-g428490e3 #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 <49> 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: <stable@vger.kernel.org> # v2.6.12+
      Reported-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      cc: Eric Biggers <ebiggers@google.com>
      60ff5b2f
    • David Howells's avatar
      KEYS: Fix race between updating and finding a negative key · 363b02da
      David Howells authored
      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: 146aa8b1 ("KEYS: Merge the type-specific data with the payload data")
      Cc: stable@vger.kernel.org # v4.4+
      Reported-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      Reviewed-by: 's avatarEric Biggers <ebiggers@google.com>
      363b02da
    • Arnd Bergmann's avatar
      security/keys: BIG_KEY requires CONFIG_CRYPTO · 3cd18d19
      Arnd Bergmann authored
      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: 428490e3 ("security/keys: rewrite all of big_key crypto")
      Signed-off-by: 's avatarArnd Bergmann <arnd@arndb.de>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      3cd18d19
  8. 12 Oct, 2017 1 commit
    • Eric Biggers's avatar
      KEYS: encrypted: fix dereference of NULL user_key_payload · 13923d08
      Eric Biggers authored
      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: 7e70cb49 ("keys: add new key-type encrypted")
      Reviewed-by: 's avatarJames Morris <james.l.morris@oracle.com>
      Cc: <stable@vger.kernel.org>    [v2.6.38+]
      Cc: Mimi Zohar <zohar@linux.vnet.ibm.com>
      Cc: David Safford <safford@us.ibm.com>
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      13923d08
  9. 04 Oct, 2017 1 commit
    • Casey Schaufler's avatar
      lsm: fix smack_inode_removexattr and xattr_getsecurity memleak · 57e7ba04
      Casey Schaufler authored
      security_inode_getsecurity() provides the text string value
      of a security attribute. It does not provide a "secctx".
      The code in xattr_getsecurity() that calls security_inode_getsecurity()
      and then calls security_release_secctx() happened to work because
      SElinux and Smack treat the attribute and the secctx the same way.
      It fails for cap_inode_getsecurity(), because that module has no
      secctx that ever needs releasing. It turns out that Smack is the
      one that's doing things wrong by not allocating memory when instructed
      to do so by the "alloc" parameter.
      
      The fix is simple enough. Change the security_release_secctx() to
      kfree() because it isn't a secctx being returned by
      security_inode_getsecurity(). Change Smack to allocate the string when
      told to do so.
      
      Note: this also fixes memory leaks for LSMs which implement
      inode_getsecurity but not release_secctx, such as capabilities.
      Signed-off-by: 's avatarCasey Schaufler <casey@schaufler-ca.com>
      Reported-by: 's avatarKonstantin Khlebnikov <khlebnikov@yandex-team.ru>
      Cc: stable@vger.kernel.org
      Signed-off-by: 's avatarJames Morris <james.l.morris@oracle.com>
      57e7ba04
  10. 25 Sep, 2017 12 commits
    • Jason A. Donenfeld's avatar
      security/keys: rewrite all of big_key crypto · 428490e3
      Jason A. Donenfeld authored
      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: 's avatarJason A. Donenfeld <Jason@zx2c4.com>
      Reviewed-by: 's avatarEric Biggers <ebiggers3@gmail.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      Cc: Herbert Xu <herbert@gondor.apana.org.au>
      Cc: Kirill Marinushkin <k.marinushkin@gmail.com>
      Cc: security@kernel.org
      Cc: stable@vger.kernel.org
      428490e3
    • Jason A. Donenfeld's avatar
      security/keys: properly zero out sensitive key material in big_key · 91080180
      Jason A. Donenfeld authored
      Error paths forgot to zero out sensitive material, so this patch changes
      some kfrees into a kzfrees.
      Signed-off-by: 's avatarJason A. Donenfeld <Jason@zx2c4.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      Reviewed-by: 's avatarEric Biggers <ebiggers3@gmail.com>
      Cc: Herbert Xu <herbert@gondor.apana.org.au>
      Cc: Kirill Marinushkin <k.marinushkin@gmail.com>
      Cc: security@kernel.org
      Cc: stable@vger.kernel.org
      91080180
    • Eric Biggers's avatar
      KEYS: use kmemdup() in request_key_auth_new() · e007ce9c
      Eric Biggers authored
      kmemdup() is preferred to kmalloc() followed by memcpy().
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      e007ce9c
    • Eric Biggers's avatar
      KEYS: restrict /proc/keys by credentials at open time · 4aa68e07
      Eric Biggers authored
      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 34dbbcdb ("Make file credentials available to the
      seqfile interfaces") we can finally fix it.  So let's do it.
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      4aa68e07
    • Eric Biggers's avatar
      KEYS: reset parent each time before searching key_user_tree · 8f674565
      Eric Biggers authored
      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: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      8f674565
    • Eric Biggers's avatar
      KEYS: prevent KEYCTL_READ on negative key · 37863c43
      Eric Biggers authored
      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 <0f> 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: 61ea0c0b ("KEYS: Skip key state checks when checking for possession")
      Cc: <stable@vger.kernel.org>	[v3.13+]
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      37863c43
    • Eric Biggers's avatar
      KEYS: prevent creating a different user's keyrings · 237bbd29
      Eric Biggers authored
      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: 69664cf1 ("keys: don't generate user and user session keyrings unless they're accessed")
      Cc: <stable@vger.kernel.org>	[v2.6.26+]
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      237bbd29
    • Eric Biggers's avatar
      KEYS: fix writing past end of user-supplied buffer in keyring_read() · e645016a
      Eric Biggers authored
      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: b2a4df20 ("KEYS: Expand the capacity of a keyring")
      Cc: <stable@vger.kernel.org>	[v3.13+]
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      e645016a
    • Eric Biggers's avatar
      KEYS: fix key refcount leak in keyctl_read_key() · 7fc0786d
      Eric Biggers authored
      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: 29db9190 ("[PATCH] Keys: Add LSM hooks for key management [try #3]")
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      7fc0786d
    • Eric Biggers's avatar
      KEYS: fix key refcount leak in keyctl_assume_authority() · 884bee02
      Eric Biggers authored
      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: d84f4f99 ("CRED: Inaugurate COW credentials")
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      884bee02
    • Eric Biggers's avatar
      KEYS: don't revoke uninstantiated key in request_key_auth_new() · f7b48cf0
      Eric Biggers authored
      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: b5f545c8 ("[PATCH] keys: Permit running process to instantiate keys")
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      f7b48cf0
    • Eric Biggers's avatar
      KEYS: fix cred refcount leak in request_key_auth_new() · 44d81433
      Eric Biggers authored
      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: d84f4f99 ("CRED: Inaugurate COW credentials")
      Signed-off-by: 's avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: 's avatarDavid Howells <dhowells@redhat.com>
      44d81433
  11. 24 Sep, 2017 1 commit
  12. 22 Sep, 2017 11 commits