Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp757862ybl; Wed, 4 Dec 2019 10:21:09 -0800 (PST) X-Google-Smtp-Source: APXvYqwc1/GewmqMjJrC9lUMP6YSON9zNZXpFwbdC63t2DTKtA0vnDLIQLbwNPJ/GZHlzBDcGKZT X-Received: by 2002:a9d:6315:: with SMTP id q21mr3404278otk.15.1575483669154; Wed, 04 Dec 2019 10:21:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575483669; cv=none; d=google.com; s=arc-20160816; b=kz/xVLYvhvb86tuZJstqoa7ZbGz8Jpghvi3L+qvTb/K+jkTgMRtIjTCv26iFgco+5A sRUTMWSjRjy3TQ9KHyM/qvJrtKqtZRadPsQfJkNYTF3vm6AjEVhEstgAhkRYRRzlYhOu uehdQ9MGNLw5ZziOKsl405G++aBiFrK8u7kO1ptP9I6BDczDiZ566toNEWlFb20HGaXv 5kNw5qWVQZU/SfRUxHzZ9KQKZpv2EQRbA9O4SIaYmXUznYRWP2Kfeuw4VQQ/oi0br85Y vIEvK9D08/v9ifo6BPkoW6vqrB0aFagOMd87/1w0i5mMPM7tl0l8MSfI6YhzH+sw1x6p s7/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UKPd0sOsNb8ZYYWFDtILQke3by4dEC5jpAlsdCGlnts=; b=dhbGEqJ/go9VTnQ3Q/PLZgmuXxl2Q+wSnZKV61mtGrVFcQCBrXQ1ZCeNwOj9xhweC9 QRHRHOdzeVLyaewKaQuDtA03B5p9EzD4WS6FDge7feTOlo3zQrRohn18mbCOz3vEG6fK uq0uxYIAthwdFo9ZJsOAwzqd+azi1TgGjOsLNRJmIl/121AsilHUlEt/zq5SVLGOQnM8 kCkju8o36ecdgZNJuEQ/z6HoYRfIqgeJ7LMI3AIspR0+3cZVUSdihFCpmZV7Y4RW9JFK UZr/p8nGhUSh5XRwPTW+qfEQaHp33t5rbH3ZirySVbpVCDGxf566Uj33hlpVrhHCe6y5 2L/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LGOyeImD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d13si3588519otp.196.2019.12.04.10.20.54; Wed, 04 Dec 2019 10:21:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LGOyeImD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731858AbfLDSSq (ORCPT + 99 others); Wed, 4 Dec 2019 13:18:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:37914 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731274AbfLDSKe (ORCPT ); Wed, 4 Dec 2019 13:10:34 -0500 Received: from localhost (unknown [217.68.49.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C94E820675; Wed, 4 Dec 2019 18:10:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575483033; bh=AfZKqJHeZ2R2QINf8sSlLG5/pEKH8/9gZ7eadsA2v0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LGOyeImDmHLlXEkKmDmMZ+ovmSbFqmFfFMjfvybqjkiZ66Kq730FaQwSJQhouj6+5 H9UWMbrsyldUo452EpTaqgYX6g3KdhW+Um6Pr6k2HJGywQ4bimz0q//R+3KoRhHBCI 6c8vfHn11ydw23jMncTHG6/0zzA6WWG9/0zlIDDM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Herbert Xu , Sasha Levin Subject: [PATCH 4.9 024/125] crypto: user - support incremental algorithm dumps Date: Wed, 4 Dec 2019 18:55:29 +0100 Message-Id: <20191204175319.287249039@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191204175308.377746305@linuxfoundation.org> References: <20191204175308.377746305@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers [ Upstream commit 0ac6b8fb23c724b015d9ca70a89126e8d1563166 ] CRYPTO_MSG_GETALG in NLM_F_DUMP mode sometimes doesn't return all registered crypto algorithms, because it doesn't support incremental dumps. crypto_dump_report() only permits itself to be called once, yet the netlink subsystem allocates at most ~64 KiB for the skb being dumped to. Thus only the first recvmsg() returns data, and it may only include a subset of the crypto algorithms even if the user buffer passed to recvmsg() is large enough to hold all of them. Fix this by using one of the arguments in the netlink_callback structure to keep track of the current position in the algorithm list. Then userspace can do multiple recvmsg() on the socket after sending the dump request. This is the way netlink dumps work elsewhere in the kernel; it's unclear why this was different (probably just an oversight). Also fix an integer overflow when calculating the dump buffer size hint. Fixes: a38f7907b926 ("crypto: Add userspace configuration API") Signed-off-by: Eric Biggers Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- crypto/crypto_user.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/crypto/crypto_user.c b/crypto/crypto_user.c index c90a1727cd2c6..60cf7d163731b 100644 --- a/crypto/crypto_user.c +++ b/crypto/crypto_user.c @@ -277,30 +277,33 @@ drop_alg: static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb) { - struct crypto_alg *alg; + const size_t start_pos = cb->args[0]; + size_t pos = 0; struct crypto_dump_info info; - int err; - - if (cb->args[0]) - goto out; - - cb->args[0] = 1; + struct crypto_alg *alg; + int res; info.in_skb = cb->skb; info.out_skb = skb; info.nlmsg_seq = cb->nlh->nlmsg_seq; info.nlmsg_flags = NLM_F_MULTI; + down_read(&crypto_alg_sem); list_for_each_entry(alg, &crypto_alg_list, cra_list) { - err = crypto_report_alg(alg, &info); - if (err) - goto out_err; + if (pos >= start_pos) { + res = crypto_report_alg(alg, &info); + if (res == -EMSGSIZE) + break; + if (res) + goto out; + } + pos++; } - + cb->args[0] = pos; + res = skb->len; out: - return skb->len; -out_err: - return err; + up_read(&crypto_alg_sem); + return res; } static int crypto_dump_report_done(struct netlink_callback *cb) @@ -483,7 +486,7 @@ static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) && (nlh->nlmsg_flags & NLM_F_DUMP))) { struct crypto_alg *alg; - u16 dump_alloc = 0; + unsigned long dump_alloc = 0; if (link->dump == NULL) return -EINVAL; @@ -491,16 +494,16 @@ static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) down_read(&crypto_alg_sem); list_for_each_entry(alg, &crypto_alg_list, cra_list) dump_alloc += CRYPTO_REPORT_MAXSIZE; + up_read(&crypto_alg_sem); { struct netlink_dump_control c = { .dump = link->dump, .done = link->done, - .min_dump_alloc = dump_alloc, + .min_dump_alloc = min(dump_alloc, 65535UL), }; err = netlink_dump_start(crypto_nlsk, skb, nlh, &c); } - up_read(&crypto_alg_sem); return err; } -- 2.20.1