Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2863713pxb; Tue, 9 Mar 2021 12:55:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2G2K2YyQ80Bm8ov6bzpl8ydmVT1jWBe+fjjRxAO5gf6Tf6xbjE/n+KOEHhnpxqHggbYuz X-Received: by 2002:a17:906:5e50:: with SMTP id b16mr22925827eju.272.1615323306164; Tue, 09 Mar 2021 12:55:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615323306; cv=none; d=google.com; s=arc-20160816; b=BwDQqFk8ss2Nd5fVr3/Yi/z1W8mZa4GX/7HaHh0ONL/DJcdjheeWwOLSaeNjewkyN9 YfifX3Vi5FqBCUlaGNE11F9SsFeIzAX4lB143qxKRF8pmAVEIg9aGQ02UgB1lJ2ZTcmI zzsDbt/wfsckf9SzHpQMRcw2eoxFxxLVQT/CB1/+AYhbCr7mgE9MippDF2niP3O0Z9/k l1vIRxtGoD/J36LvBkDlzL1wd8eQytWwpsUJyYeTpOgy4LEF/yRSvoS/zt8xgjAOaSpZ FXpsXutm6EsvGC3YLFwiYlgnsSJ3cKq36kb/h2Gms9OmSPnRLCyMtlAzE/lq/q7UErj6 wRPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=gWcqe+g4PYvymlA7EinAD2uSQgU+3l68r0HRW1HJRww=; b=ocG8vM75Zfe36GumcSNWYKeMgSuuSEehIfoXUGzA8MMYe9jI4UHU0SkKDKi590UGc/ FHfIAhFCAt40O0Mz67nB1T6OCbG8DQkB7sp0VDIqso/GLiLlz8k261lvlVDJ2KAbn1C1 0fnMzqfJUQSnzYfhhoLZIynhD+fiPkZAbpPG2YlMET1MYq26r97RBUSHWTofSyTzOci9 NCZMujDgXD+ham6AwuUZtyczwWRCKvC/fiay3XaPXhj5vzARcz325BkcwCu9BwVayFF6 U353Zou2zo3Vi5NWQwuvoCH4KjiY9Ys+ebJq+zOeCFkIs1NYT2eafCD1mq6s8oWdA/vO 0dyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=crrbLNlQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i3si10056691ejy.441.2021.03.09.12.54.42; Tue, 09 Mar 2021 12:55:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=crrbLNlQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231904AbhCIUvk (ORCPT + 99 others); Tue, 9 Mar 2021 15:51:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231858AbhCIUvN (ORCPT ); Tue, 9 Mar 2021 15:51:13 -0500 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B94F0C06174A for ; Tue, 9 Mar 2021 12:51:12 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id mm21so31537749ejb.12 for ; Tue, 09 Mar 2021 12:51:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gWcqe+g4PYvymlA7EinAD2uSQgU+3l68r0HRW1HJRww=; b=crrbLNlQsrwE5LqsAy+1poy/6Ac6lfVlWoix+jdBu3iWgoNmnANhkpg8cyIQf9GUOE bYjtzXXamzIoCBBS/cfqfNJglpksRte2VZDslKuNjWe/4xuhwspYqUOHhSdZQ1Rfoinp AxNT4t1fw9R3Pk370D2M/454sCONqE7vTewfo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gWcqe+g4PYvymlA7EinAD2uSQgU+3l68r0HRW1HJRww=; b=CPyAGC4MPcicztQpibwiBKzEPXn7qIXLMKOJsNlvJfIO5hkW/OGLjicec39UxoWPbp qlTv6g4TCy5VoknfB94ATI0MoCwv5Dbk/ioC0cQCZJfzfLjMSnZl8U9mvkyOmfh+Y60L ZiQcx7HjRcE1bOcoIpOmUsKZjyfwLTWbN8xggVbh/PBTUFQx6myaqL7RRtYOKRD55eI4 /XAfZ5Gk2uxiU8hH9ZLw/zmQtHmn3BDH80ZgOJXwZtC5v/lZjwTC4BuCdw2+8cmOiasZ 5jeCI/65Vdlzop3GrHY6fM0TvX2RzN8WQtzyV0gNybtfPuUg8Z8Njt6EOXbFcal0x2yt 9E+A== X-Gm-Message-State: AOAM533qA6rLSDn7664ydsaShtg+9ycftU30VpCGOTLAuYzmJIq5Z9b2 N/p7pC3eM/NuRcA+a7PlwDkTPw== X-Received: by 2002:a17:906:150f:: with SMTP id b15mr13427176ejd.363.1615323071549; Tue, 09 Mar 2021 12:51:11 -0800 (PST) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id mc10sm8861190ejb.56.2021.03.09.12.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 12:51:11 -0800 (PST) From: Ricardo Ribalda To: Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Daniel Vetter , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ricardo Ribalda , stable@vger.kernel.org Subject: [PATCH 1/2] lib/scatterlist: Fix NULL pointer deference Date: Tue, 9 Mar 2021 21:51:07 +0100 Message-Id: <20210309205108.997166-1-ribalda@chromium.org> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When sg_alloc_table_from_pages is called with n_pages = 0, we write in a non-allocated page. Fix it by checking early the error condition. [ 7.666801] BUG: kernel NULL pointer dereference, address: 0000000000000010 [ 7.667487] #PF: supervisor read access in kernel mode [ 7.667970] #PF: error_code(0x0000) - not-present page [ 7.668448] PGD 0 P4D 0 [ 7.668690] Oops: 0000 [#1] SMP NOPTI [ 7.669037] CPU: 0 PID: 184 Comm: modprobe Not tainted 5.11.0+ #2 [ 7.669606] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 [ 7.670378] RIP: 0010:__sg_alloc_table_from_pages+0x2c5/0x4a0 [ 7.670924] Code: c9 01 48 c7 40 08 00 00 00 00 48 89 08 8b 47 0c 41 8d 44 00 ff 89 47 0c 48 81 fa 00 f0 ff ff 0f 87 d4 01 00 00 49 8b 16 89 d8 <4a> 8b 74 fd 00 4c 89 d1 44 29 f8 c1 e0 0c 44 29 d8 4c 39 d0 48 0f [ 7.672643] RSP: 0018:ffffba1e8028fb30 EFLAGS: 00010287 [ 7.673133] RAX: 0000000000000001 RBX: 0000000000000001 RCX: 0000000000000002 [ 7.673791] RDX: 0000000000000002 RSI: ffffffffada6d0ba RDI: ffff9afe01fff820 [ 7.674448] RBP: 0000000000000010 R08: 0000000000000001 R09: 0000000000000001 [ 7.675100] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000 [ 7.675754] R13: 00000000fffff000 R14: ffff9afe01fff800 R15: 0000000000000000 [ 7.676409] FS: 00007fb0f448f540(0000) GS:ffff9afe07a00000(0000) knlGS:0000000000000000 [ 7.677151] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 7.677681] CR2: 0000000000000010 CR3: 0000000002184001 CR4: 0000000000370ef0 [ 7.678342] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 7.679019] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 7.680349] Call Trace: [ 7.680605] ? device_add+0x146/0x810 [ 7.681021] sg_alloc_table_from_pages+0x11/0x30 [ 7.681511] vb2_dma_sg_alloc+0x162/0x280 [videobuf2_dma_sg] Cc: stable@vger.kernel.org Fixes: efc42bc98058 ("scatterlist: add sg_alloc_table_from_pages function") Signed-off-by: Ricardo Ribalda --- lib/scatterlist.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/scatterlist.c b/lib/scatterlist.c index a59778946404..1e83b6a3d930 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -435,6 +435,9 @@ struct scatterlist *__sg_alloc_table_from_pages(struct sg_table *sgt, unsigned int added_nents = 0; struct scatterlist *s = prv; + if (n_pages == 0) + return ERR_PTR(-EINVAL); + /* * The algorithm below requires max_segment to be aligned to PAGE_SIZE * otherwise it can overshoot. -- 2.30.1.766.gb4fecdf3b7-goog