Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1985476lqb; Mon, 27 May 2024 04:24:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVgWUZ+WE0FCyGasrp2YcvT4iCSGnherqmhB2x13oAarGNYM2v15r5ooYjdGibRbPTTq8iBc0vlF42TQC58b5nGtVCVOZWt61eXqK/gYg== X-Google-Smtp-Source: AGHT+IFUXX4syh3blv0BsldKM+jQk1obEsjhkrbFcmIO5lAq9fnF/yL8bAN4XKGEvA10IzUT89cH X-Received: by 2002:a05:6820:1c9c:b0:5b7:1979:ec49 with SMTP id 006d021491bc7-5b95c5dea19mr8665726eaf.0.1716809050351; Mon, 27 May 2024 04:24:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716809050; cv=pass; d=google.com; s=arc-20160816; b=LIUfzDy1zfVfwH+Zlg6w1ZPC1tAF59Kry0DEUGNKWfOs2H7JmpBw2Wyal5wbn9D+HQ 2Wx4f+y/IRnCIACD0MQGTm/mYOg/jVMSARrRkkQYIVxAxiojcFgouJY463ULmfVRetYx 4TaW3lcyF52oa3tJqXlB/dW8HKP9uMjpCTEUPDlyQg0SVYzqOt3ePQeZk0evJhovxH20 1xLxxJbjNLrRsorvSQ2JE0qYT75UVEsJ226GSpl7UuNldas1IRNgWic2r/n87ezWiBVt YfqJkFHKUn5uX1C81YaqnfszD/efkeDEdhy3oaTxhftr7EjhjWc4M1p1jPUJyoroG5gn G5ZA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:cc:to:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=ezJXdyF0CJ1UVaUvAyDpaA8e9pPvbwoD8x8nXFabkEM=; fh=flEHkodX0u8f1FVlg2l2UyHwqL94YmY5Hwz7lukzuP4=; b=NAvmLlmyeaQXrweMkt1oX/Xpp4trRSz7NpgOl0ekl6nmgSTp2lwDxGzIRL5yzoJ7rm gmmrBpoqLgsWDhyj6oFdcp9SXDoL3CNRFSiynbVxnanyeJNdQ+z+Us8dv8vMOuzsjbTg ytq8z6JqEe8tu2BPg6QVLbr9VdempVRYsXNgxq9aceHhejXlw6aVBlg4Zp0MfzsVrjDj 8SAr/JL6KufM13rFsfQfI59FutQapfHy3IrgtssfP0/D+RARrACRva/ekGsZJUnkhH6V wPO6tdJ2txHBl9j7/eymedEwBvR1Ozv3W8ODXK2OXa4rjJnZZ5xRra0d8j0LQfsHfjmr RAXg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gyVltNGu; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-190484-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190484-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43fb18cf7bdsi75784991cf.707.2024.05.27.04.24.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 04:24:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190484-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gyVltNGu; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-190484-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190484-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 07A7F1C20349 for ; Mon, 27 May 2024 11:24:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44E2214F12E; Mon, 27 May 2024 11:24:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gyVltNGu" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7ED5114EC6B for ; Mon, 27 May 2024 11:24:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716809043; cv=none; b=QzRpyEXLKFdZEl3tgBCk/XGt+gFmTw7+AYnd1/kpM4ZYV71aWR6qzS445xzizwuTluXQA2njTRUTE5UhUnFqc2C/ZhyLCAyXyzufykI9sgA5CK+3cyDu2wRB2maSIrU3WzSuovI5UVbZ+5Pj/5pOhlVPu3R//ub0CU4orz/3xPE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716809043; c=relaxed/simple; bh=I1jeBlgpNjWM5V+1t6DmYPIGjHTRRjbnXfam3eEIuDA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=F92LTUuNT+4HHYHtRsIJ0P0TaVmzDl/QlBiRpedLHMJC6XFw0LWDPiqNU6mddh/zbve8EF4if2Ezkz0p1YqjHrEx5Cj2YQEwLJLSvREVPW/KK9t7SHfYdZyz0Zc7m7YuGh/K3LG+dHajNZREtJabpQ3q9Q7P+UVB4otxN62cl9o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gyVltNGu; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716809040; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ezJXdyF0CJ1UVaUvAyDpaA8e9pPvbwoD8x8nXFabkEM=; b=gyVltNGuPrAMQhZz8Q0NmHAbYIOCm1F5Ru6+4MJUZU0aqqcyiS+tuDhbNIdBejc5Tsi8Lv gPhGaF1XvGm4oppyVO4dO1o0VmYtKxjPL4HmBVOCeh9X4oyAKT+m+JqtNthw8K99TsD+r9 RBTzHUA2tEaiPXPJpAehwsNNpH4dZJ8= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-63bF6eYWNluJon4DL7PHSg-1; Mon, 27 May 2024 07:23:59 -0400 X-MC-Unique: 63bF6eYWNluJon4DL7PHSg-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4211211e18cso6043735e9.3 for ; Mon, 27 May 2024 04:23:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716809036; x=1717413836; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ezJXdyF0CJ1UVaUvAyDpaA8e9pPvbwoD8x8nXFabkEM=; b=iTITveeSqFeZ794f8UnivVpDhfgifwARwfhz8GyAJ/PGlwER2gFZdEdDVXVrY3NZQ6 HaQcbGRkoMlFF8O7WeT/CC4IC+puDyb0fn8bPyzUxMzE3Iw7mUuMiqj/8EOI+pcQ8RL7 SgW2gJWsidey/EWnTIY51ZvsdgNLY7ZycO2GQbphJ6WSbdPzmFXHmknWjsKMKUl6uH9x RmRIl3kVPRyEJnMFqOMa8Up0wolTnecry2ezDtWtX5CXrZNlLB/Ac9vZNnahPfe9WLu1 Sr0U1e2DTmBtMJ3jQl+zsDCCOY8Kj9rm6WHU6D+/5vpDCQiJS+Z89gt4A6xhfBQ0JyOL GPeA== X-Forwarded-Encrypted: i=1; AJvYcCWRpAR9ZSbO3fXncrwq/elniYrGw+Qzvht0E11hmMaM8nKQwjFPsOaG9phYgKP1xl7AjDYFpHMIj9+vqALPDMhKowUiZK/yA/8l4SeY X-Gm-Message-State: AOJu0YzWNI6HDmlQxcMyKJUjBl5eSL+IH7te6PUVes2iuqOS6vmo99Lg Y8+vN/1gFsvbAmz+hRX4oIjZ9LG9kMUBo9faNUaQe0bxJA+fq9s1QhS6GIxP566CarukMsUVf+m HTxC9+x6cTiW77XyWSBGle97nzHV8yMrtvNJBkuQuwMgnx3sLvPNAm2n1dTTI1Q== X-Received: by 2002:a05:600c:474f:b0:421:10e4:7f6b with SMTP id 5b1f17b1804b1-42110e47fd9mr36926245e9.27.1716809036240; Mon, 27 May 2024 04:23:56 -0700 (PDT) X-Received: by 2002:a05:600c:474f:b0:421:10e4:7f6b with SMTP id 5b1f17b1804b1-42110e47fd9mr36926025e9.27.1716809035839; Mon, 27 May 2024 04:23:55 -0700 (PDT) Received: from ?IPV6:2a02:810d:4b3f:ee94:abf:b8ff:feee:998b? ([2a02:810d:4b3f:ee94:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-421089708f8sm105016995e9.20.2024.05.27.04.23.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 May 2024 04:23:55 -0700 (PDT) Message-ID: <59d4bf96-9a94-4e2c-9636-81e79d572438@redhat.com> Date: Mon, 27 May 2024 13:23:53 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] drm/nouveau/nvif: Avoid build error due to potential integer overflows To: Guenter Roeck Cc: Karol Herbst , Lyude Paul , Daniel Vetter , dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org, Javier Martinez Canillas , Jani Nikula , Thomas Zimmermann , Maxime Ripard , Kees Cook , Christophe JAILLET , Joe Perches , David Airlie References: <20240524134817.1369993-1-linux@roeck-us.net> Content-Language: en-US From: Danilo Krummrich Organization: RedHat In-Reply-To: <20240524134817.1369993-1-linux@roeck-us.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 5/24/24 15:48, Guenter Roeck wrote: > Trying to build parisc:allmodconfig with gcc 12.x or later results > in the following build error. > > drivers/gpu/drm/nouveau/nvif/object.c: In function 'nvif_object_mthd': > drivers/gpu/drm/nouveau/nvif/object.c:161:9: error: > 'memcpy' accessing 4294967264 or more bytes at offsets 0 and 32 overlaps 6442450881 bytes at offset -2147483617 [-Werror=restrict] > 161 | memcpy(data, args->mthd.data, size); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > drivers/gpu/drm/nouveau/nvif/object.c: In function 'nvif_object_ctor': > drivers/gpu/drm/nouveau/nvif/object.c:298:17: error: > 'memcpy' accessing 4294967240 or more bytes at offsets 0 and 56 overlaps 6442450833 bytes at offset -2147483593 [-Werror=restrict] > 298 | memcpy(data, args->new.data, size); > > gcc assumes that 'sizeof(*args) + size' can overflow, which would result > in the problem. > > The problem is not new, only it is now no longer a warning but an error > since W=1 has been enabled for the drm subsystem and since Werror is > enabled for test builds. > > Rearrange arithmetic and use check_add_overflow() for validating the > allocation size to avoid the overflow. While at it, split assignments > out of if conditions. > > Fixes: a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem") > Cc: Javier Martinez Canillas > Cc: Jani Nikula > Cc: Thomas Zimmermann > Cc: Danilo Krummrich > Cc: Maxime Ripard > Cc: Kees Cook > Cc: Christophe JAILLET > Cc: Joe Perches > Reviewed-by: Kees Cook > Signed-off-by: Guenter Roeck Applied to drm-misc-fixes, thanks! > --- > v3: Split assignments from if conditions. > v2: Use check_add_overflow() to calculate the allocation size and to check > for overflows. > > drivers/gpu/drm/nouveau/nvif/object.c | 24 ++++++++++++++++++------ > 1 file changed, 18 insertions(+), 6 deletions(-) > > diff --git a/drivers/gpu/drm/nouveau/nvif/object.c b/drivers/gpu/drm/nouveau/nvif/object.c > index 4d1aaee8fe15..1d19c87eaec1 100644 > --- a/drivers/gpu/drm/nouveau/nvif/object.c > +++ b/drivers/gpu/drm/nouveau/nvif/object.c > @@ -142,11 +142,16 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) > struct nvif_ioctl_v0 ioctl; > struct nvif_ioctl_mthd_v0 mthd; > } *args; > + u32 args_size; > u8 stack[128]; > int ret; > > - if (sizeof(*args) + size > sizeof(stack)) { > - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) > + if (check_add_overflow(sizeof(*args), size, &args_size)) > + return -ENOMEM; > + > + if (args_size > sizeof(stack)) { > + args = kmalloc(args_size, GFP_KERNEL); > + if (!args) > return -ENOMEM; > } else { > args = (void *)stack; > @@ -157,7 +162,7 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) > args->mthd.method = mthd; > > memcpy(args->mthd.data, data, size); > - ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); > + ret = nvif_object_ioctl(object, args, args_size, NULL); > memcpy(data, args->mthd.data, size); > if (args != (void *)stack) > kfree(args); > @@ -276,7 +281,15 @@ nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle, > object->map.size = 0; > > if (parent) { > - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) { > + u32 args_size; > + > + if (check_add_overflow(sizeof(*args), size, &args_size)) { > + nvif_object_dtor(object); > + return -ENOMEM; > + } > + > + args = kmalloc(args_size, GFP_KERNEL); > + if (!args) { > nvif_object_dtor(object); > return -ENOMEM; > } > @@ -293,8 +306,7 @@ nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle, > args->new.oclass = oclass; > > memcpy(args->new.data, data, size); > - ret = nvif_object_ioctl(parent, args, sizeof(*args) + size, > - &object->priv); > + ret = nvif_object_ioctl(parent, args, args_size, &object->priv); > memcpy(data, args->new.data, size); > kfree(args); > if (ret == 0)