17 Feb, 2018

1 commit

  • commit 9fa68f620041be04720d0cbfb1bd3ddfc6310b24 upstream.

    Currently, almost none of the keyed hash algorithms check whether a key
    has been set before proceeding. Some algorithms are okay with this and
    will effectively just use a key of all 0's or some other bogus default.
    However, others will severely break, as demonstrated using
    "hmac(sha3-512-generic)", the unkeyed use of which causes a kernel crash
    via a (potentially exploitable) stack buffer overflow.

    A while ago, this problem was solved for AF_ALG by pairing each hash
    transform with a 'has_key' bool. However, there are still other places
    in the kernel where userspace can specify an arbitrary hash algorithm by
    name, and the kernel uses it as unkeyed hash without checking whether it
    is really unkeyed. Examples of this include:

    - KEYCTL_DH_COMPUTE, via the KDF extension
    - dm-verity
    - dm-crypt, via the ESSIV support
    - dm-integrity, via the "internal hash" mode with no key given
    - drbd (Distributed Replicated Block Device)

    This bug is especially bad for KEYCTL_DH_COMPUTE as that requires no
    privileges to call.

    Fix the bug for all users by adding a flag CRYPTO_TFM_NEED_KEY to the
    ->crt_flags of each hash transform that indicates whether the transform
    still needs to be keyed or not. Then, make the hash init, import, and
    digest functions return -ENOKEY if the key is still needed.

    The new flag also replaces the 'has_key' bool which algif_hash was
    previously using, thereby simplifying the algif_hash implementation.

    Reported-by: syzbot
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu
    Signed-off-by: Greg Kroah-Hartman

    Eric Biggers
     

10 Mar, 2017

1 commit

  • Lockdep issues a circular dependency warning when AFS issues an operation
    through AF_RXRPC from a context in which the VFS/VM holds the mmap_sem.

    The theory lockdep comes up with is as follows:

    (1) If the pagefault handler decides it needs to read pages from AFS, it
    calls AFS with mmap_sem held and AFS begins an AF_RXRPC call, but
    creating a call requires the socket lock:

    mmap_sem must be taken before sk_lock-AF_RXRPC

    (2) afs_open_socket() opens an AF_RXRPC socket and binds it. rxrpc_bind()
    binds the underlying UDP socket whilst holding its socket lock.
    inet_bind() takes its own socket lock:

    sk_lock-AF_RXRPC must be taken before sk_lock-AF_INET

    (3) Reading from a TCP socket into a userspace buffer might cause a fault
    and thus cause the kernel to take the mmap_sem, but the TCP socket is
    locked whilst doing this:

    sk_lock-AF_INET must be taken before mmap_sem

    However, lockdep's theory is wrong in this instance because it deals only
    with lock classes and not individual locks. The AF_INET lock in (2) isn't
    really equivalent to the AF_INET lock in (3) as the former deals with a
    socket entirely internal to the kernel that never sees userspace. This is
    a limitation in the design of lockdep.

    Fix the general case by:

    (1) Double up all the locking keys used in sockets so that one set are
    used if the socket is created by userspace and the other set is used
    if the socket is created by the kernel.

    (2) Store the kern parameter passed to sk_alloc() in a variable in the
    sock struct (sk_kern_sock). This informs sock_lock_init(),
    sock_init_data() and sk_clone_lock() as to the lock keys to be used.

    Note that the child created by sk_clone_lock() inherits the parent's
    kern setting.

    (3) Add a 'kern' parameter to ->accept() that is analogous to the one
    passed in to ->create() that distinguishes whether kernel_accept() or
    sys_accept4() was the caller and can be passed to sk_alloc().

    Note that a lot of accept functions merely dequeue an already
    allocated socket. I haven't touched these as the new socket already
    exists before we get the parameter.

    Note also that there are a couple of places where I've made the accepted
    socket unconditionally kernel-based:

    irda_accept()
    rds_rcp_accept_one()
    tcp_accept_from_sock()

    because they follow a sock_create_kern() and accept off of that.

    Whilst creating this, I noticed that lustre and ocfs don't create sockets
    through sock_create_kern() and thus they aren't marked as for-kernel,
    though they appear to be internal. I wonder if these should do that so
    that they use the new set of lock keys.

    Signed-off-by: David Howells
    Signed-off-by: David S. Miller

    David Howells
     

27 Dec, 2016

1 commit

  • With this reproducer:
    struct sockaddr_alg alg = {
    .salg_family = 0x26,
    .salg_type = "hash",
    .salg_feat = 0xf,
    .salg_mask = 0x5,
    .salg_name = "digest_null",
    };
    int sock, sock2;

    sock = socket(AF_ALG, SOCK_SEQPACKET, 0);
    bind(sock, (struct sockaddr *)&alg, sizeof(alg));
    sock2 = accept(sock, NULL, NULL);
    setsockopt(sock, SOL_ALG, ALG_SET_KEY, "\x9b\xca", 2);
    accept(sock2, NULL, NULL);

    ==== 8< ======== 8< ======== 8< ======== 8< ====

    one can immediatelly see an UBSAN warning:
    UBSAN: Undefined behaviour in crypto/algif_hash.c:187:7
    variable length array bound value 0 ] ? __ubsan_handle_vla_bound_not_positive+0x13d/0x188
    [] ? __ubsan_handle_out_of_bounds+0x1bc/0x1bc
    [] ? hash_accept+0x5bd/0x7d0 [algif_hash]
    [] ? hash_accept_nokey+0x3f/0x51 [algif_hash]
    [] ? hash_accept_parent_nokey+0x4a0/0x4a0 [algif_hash]
    [] ? SyS_accept+0x2b/0x40

    It is a correct warning, as hash state is propagated to accept as zero,
    but creating a zero-length variable array is not allowed in C.

    Fix this as proposed by Herbert -- do "?: 1" on that site. No sizeof or
    similar happens in the code there, so we just allocate one byte even
    though we do not use the array.

    Signed-off-by: Jiri Slaby
    Cc: Herbert Xu
    Cc: "David S. Miller" (maintainer:CRYPTO API)
    Reported-by: Sasha Levin
    Signed-off-by: Herbert Xu

    Jiri Slaby
     

22 Nov, 2016

1 commit

  • Recently an init call was added to hash_recvmsg so as to reset
    the hash state in case a sendmsg call was never made.

    Unfortunately this ended up clobbering the result if the previous
    sendmsg was done with a MSG_MORE flag. This patch fixes it by
    excluding that case when we make the init call.

    Fixes: a8348bca2944 ("algif_hash - Fix NULL hash crash with shash")
    Reported-by: Patrick Steinhardt
    Signed-off-by: Herbert Xu

    Herbert Xu
     

18 Nov, 2016

1 commit

  • Recently algif_hash has been changed to allow null hashes. This
    triggers a bug when used with an shash algorithm whereby it will
    cause a crash during the digest operation.

    This patch fixes it by avoiding the digest operation and instead
    doing an init followed by a final which avoids the buggy code in
    shash.

    This patch also ensures that the result buffer is freed after an
    error so that it is not returned as a genuine hash result on the
    next recv call.

    The shash/ahash wrapper code will be fixed later to handle this
    case correctly.

    Fixes: 493b2ed3f760 ("crypto: algif_hash - Handle NULL hashes correctly")
    Signed-off-by: Herbert Xu
    Tested-by: Laura Abbott

    Herbert Xu
     

07 Sep, 2016

1 commit


30 Jan, 2016

1 commit


18 Jan, 2016

3 commits


02 Nov, 2015

1 commit

  • The hash_accept call fails to work on sockets that have not received
    any data. For some algorithm implementations it may cause crashes.

    This patch fixes this by ensuring that we only export and import on
    sockets that have received data.

    Cc: stable@vger.kernel.org
    Reported-by: Harsh Jain
    Signed-off-by: Herbert Xu
    Tested-by: Stephan Mueller

    Herbert Xu
     

12 Apr, 2015

1 commit


03 Mar, 2015

1 commit

  • After TIPC doesn't depend on iocb argument in its internal
    implementations of sendmsg() and recvmsg() hooks defined in proto
    structure, no any user is using iocb argument in them at all now.
    Then we can drop the redundant iocb argument completely from kinds of
    implementations of both sendmsg() and recvmsg() in the entire
    networking stack.

    Cc: Christoph Hellwig
    Suggested-by: Al Viro
    Signed-off-by: Ying Xue
    Signed-off-by: David S. Miller

    Ying Xue
     

04 Feb, 2015

1 commit

  • With that, all ->sendmsg() instances are converted to iov_iter primitives
    and are agnostic wrt the kind of iov_iter they are working with.
    So's the last remaining ->recvmsg() instance that wasn't kind-agnostic yet.
    All ->sendmsg() and ->recvmsg() advance ->msg_iter by the amount actually
    copied and none of them modifies the underlying iovec, etc.

    Cc: linux-crypto@vger.kernel.org
    Signed-off-by: Al Viro

    Al Viro
     

14 Dec, 2014

1 commit

  • Pull crypto update from Herbert Xu:
    - The crypto API is now documented :)
    - Disallow arbitrary module loading through crypto API.
    - Allow get request with empty driver name through crypto_user.
    - Allow speed testing of arbitrary hash functions.
    - Add caam support for ctr(aes), gcm(aes) and their derivatives.
    - nx now supports concurrent hashing properly.
    - Add sahara support for SHA1/256.
    - Add ARM64 version of CRC32.
    - Misc fixes.

    * git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: (77 commits)
    crypto: tcrypt - Allow speed testing of arbitrary hash functions
    crypto: af_alg - add user space interface for AEAD
    crypto: qat - fix problem with coalescing enable logic
    crypto: sahara - add support for SHA1/256
    crypto: sahara - replace tasklets with kthread
    crypto: sahara - add support for i.MX53
    crypto: sahara - fix spinlock initialization
    crypto: arm - replace memset by memzero_explicit
    crypto: powerpc - replace memset by memzero_explicit
    crypto: sha - replace memset by memzero_explicit
    crypto: sparc - replace memset by memzero_explicit
    crypto: algif_skcipher - initialize upon init request
    crypto: algif_skcipher - removed unneeded code
    crypto: algif_skcipher - Fixed blocking recvmsg
    crypto: drbg - use memzero_explicit() for clearing sensitive data
    crypto: drbg - use MODULE_ALIAS_CRYPTO
    crypto: include crypto- module prefix in template
    crypto: user - add MODULE_ALIAS
    crypto: sha-mb - remove a bogus NULL check
    crytpo: qat - Fix 64 bytes requests
    ...

    Linus Torvalds
     

10 Dec, 2014

1 commit

  • Note that the code _using_ ->msg_iter at that point will be very
    unhappy with anything other than unshifted iovec-backed iov_iter.
    We still need to convert users to proper primitives.

    Signed-off-by: Al Viro

    Al Viro
     

25 Nov, 2014

1 commit

  • Commit e1bd95bf7c25 ("crypto: algif - zeroize IV buffer") and
    2a6af25befd0 ("crypto: algif - zeroize message digest buffer")
    added memzero_explicit() calls on buffers that are later on
    passed back to sock_kfree_s().

    This is a discussed follow-up that, instead, extends the sock
    API and adds sock_kzfree_s(), which internally uses kzfree()
    instead of kfree() for passing the buffers back to slab.

    Having sock_kzfree_s() allows to keep the changes more minimal
    by just having a drop-in replacement instead of adding
    memzero_explicit() calls everywhere before sock_kfree_s().

    In kzfree(), the compiler is not allowed to optimize the memset()
    away and thus there's no need for memzero_explicit(). Both,
    sock_kfree_s() and sock_kzfree_s() are wrappers for
    __sock_kfree_s() and call into kfree() resp. kzfree(); here,
    __sock_kfree_s() needs to be explicitly inlined as we want the
    compiler to optimize the call and condition away and thus it
    produces e.g. on x86_64 the _same_ assembler output for
    sock_kfree_s() before and after, and thus also allows for
    avoiding code duplication.

    Cc: David S. Miller
    Signed-off-by: Daniel Borkmann
    Signed-off-by: Herbert Xu

    Daniel Borkmann
     

24 Nov, 2014

1 commit


12 Nov, 2014

1 commit


30 Nov, 2013

1 commit

  • Commit 35f9c09fe (tcp: tcp_sendpages() should call tcp_push() once)
    added an internal flag MSG_SENDPAGE_NOTLAST, similar to
    MSG_MORE.

    algif_hash, algif_skcipher, and udp used MSG_MORE from tcp_sendpages()
    and need to see the new flag as identical to MSG_MORE.

    This fixes sendfile() on AF_ALG.

    v3: also fix udp

    Cc: Tom Herbert
    Cc: Eric Dumazet
    Cc: David S. Miller
    Cc: # 3.4.x + 3.2.x
    Reported-and-tested-by: Shawn Landden
    Original-patch: Richard Weinberger
    Signed-off-by: Shawn Landden
    Signed-off-by: David S. Miller

    Shawn Landden
     

21 Nov, 2013

1 commit


10 Apr, 2013

1 commit


30 Jun, 2011

1 commit


19 Nov, 2010

1 commit

  • This patch adds the af_alg plugin for hash, corresponding to
    the ahash kernel operation type.

    Keys can optionally be set through the setsockopt interface.

    Each sendmsg call will finalise the hash unless sent with a MSG_MORE
    flag.

    Partial hash states can be cloned using accept(2).

    The interface is completely synchronous, all operations will
    complete prior to the system call returning.

    Both sendmsg(2) and splice(2) support reading the user-space
    data directly without copying (except that the Crypto API itself
    may copy the data if alignment is off).

    For now only the splice(2) interface supports performing digest
    instead of init/update/final. In future the sendmsg(2) interface
    will also be modified to use digest/finup where possible so that
    hardware that cannot return a partial hash state can still benefit
    from this interface.

    Thakns to Miloslav Trmac for reviewing this and contributing
    fixes and improvements.

    Signed-off-by: Herbert Xu
    Acked-by: David S. Miller
    Tested-by: Martin Willi

    Herbert Xu