Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3784191iob; Tue, 17 May 2022 07:12:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylKzIvg7Eu8SNTis51UAN2glRLD0aegXzAgVtA5L6qFOzvb1F4bpwRsoWjvh0U6JZ0PILY X-Received: by 2002:a65:5643:0:b0:3f2:5b19:24d2 with SMTP id m3-20020a655643000000b003f25b1924d2mr10993224pgs.599.1652796749199; Tue, 17 May 2022 07:12:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652796749; cv=none; d=google.com; s=arc-20160816; b=XN/2TzMqezg09O/IETkKK+juKgBscV45F3mX6Q4+r2rofxGBTrsbI4PeSt0gcnYBHo TttiolAnMupTAqu9+yPpvxgOJKn0jHdtfBv3Aqta52SULUcG/qI1SQC/e0JOcpWT4oCM D21N5KzbHI9Uf3bwIswTBjUl6gHlC2AR1EfLdZJT88qTIQbsKYAeoL8XF/0+y7QLb+0c EQcM0QzKWbVR6boKx+oMrg5j/qtJoE6oknrl58a248V+qdUSkQCZDjz5QOC0HNhGKrbA pmfJd7ngMrJK2I/FtaC69J/nJ1GmssCyReVeHRewOaVjLHkJDRkDm3RaXhrgFQH+5Tap Vkbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=0xxKQ63UkLF4342r+f5JL5hPNHBH+mdnNusP1wdNqn8=; b=hNOXbrg7sfEuDhU90cteWSoXehHTEB/vbHXeF0FqyzRpCrWj2IsYrfSQ9xsAeNKovh yhLW8Jr9y/JUsLr3a1/flZ/XTRk+yO1V89n2EOdRTmJqab6pd4sLBRoGtRluqn+KXjNL 81LKM1aVuWLMsh4jtKyQqL9rSHOmWwQSo+L9JjjYec36rqduLSb+YPDMDL+019B9KQPq 7Ikl/OKWl3gzb1IgB0pQ8+wDm5DF8md1xflTg4ZT4geGRbWvMVH3+o2WcrvHrU0D4y2k 6XlkkatyMI8K86btUXCJcz2QSweLSdUNWssXM43VsN2qB731RJcbg0A9KtxRcc12VVv9 GWRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Wt8ufwRT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f23-20020a17090a639700b001dbcf49fbb3si2679624pjj.50.2022.05.17.07.12.15; Tue, 17 May 2022 07:12:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Wt8ufwRT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241600AbiEQHhq (ORCPT + 99 others); Tue, 17 May 2022 03:37:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242303AbiEQHgb (ORCPT ); Tue, 17 May 2022 03:36:31 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C0F52BCB4 for ; Tue, 17 May 2022 00:36:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652772989; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=0xxKQ63UkLF4342r+f5JL5hPNHBH+mdnNusP1wdNqn8=; b=Wt8ufwRTNPyIVZVAI1XM4MtPVj3ch5eFrKV/aMYqBypoRcRamH0PMhMC2XHJQ2jkrzliPO GBPOsspFOxXv1YnaoT4XaXOGp9H8vLIrm05ES9bRytri9athLYPzptxYd/itzltJBbDtoO kDIQxSa+CcR0QSK9QOsIZU5UkjAoiMQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-252-rN0cl44rNQqwRZrePtyRQg-1; Tue, 17 May 2022 03:36:22 -0400 X-MC-Unique: rN0cl44rNQqwRZrePtyRQg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E9A19299E75D; Tue, 17 May 2022 07:36:21 +0000 (UTC) Received: from asgard.redhat.com (unknown [10.36.110.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0443E5739A1; Tue, 17 May 2022 07:36:17 +0000 (UTC) Date: Tue, 17 May 2022 09:36:15 +0200 From: Eugene Syromiatnikov To: Jiri Olsa , Masami Hiramatsu , Steven Rostedt , Ingo Molnar , Alexei Starovoitov , Daniel Borkmann Cc: Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 1/4] bpf_trace: check size for overflow in bpf_kprobe_multi_link_attach Message-ID: <9e4171972a3d75e656073e0c25cd4071a6f652e4.1652772731.git.esyr@redhat.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Check that size would not overflow before calculation (and return -EOVERFLOW if it will), to prevent potential out-of-bounds write with the following copy_from_user. Use kvmalloc_array in copy_user_syms to prevent out-of-bounds write into syms (and especially buf) as well. Fixes: 0dcac272540613d4 ("bpf: Add multi kprobe link") Cc: # 5.18 Signed-off-by: Eugene Syromiatnikov --- kernel/trace/bpf_trace.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 7141ca8..9c041be 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2261,11 +2261,11 @@ static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 int err = -ENOMEM; unsigned int i; - syms = kvmalloc(cnt * sizeof(*syms), GFP_KERNEL); + syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL); if (!syms) goto error; - buf = kvmalloc(cnt * KSYM_NAME_LEN, GFP_KERNEL); + buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL); if (!buf) goto error; @@ -2461,7 +2461,8 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr if (!cnt) return -EINVAL; - size = cnt * sizeof(*addrs); + if (check_mul_overflow(cnt, (u32)sizeof(*addrs), &size)) + return -EOVERFLOW; addrs = kvmalloc(size, GFP_KERNEL); if (!addrs) return -ENOMEM; -- 2.1.4