Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp956849pxb; Thu, 26 Aug 2021 20:11:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxb647OP9PKC5Oxl/U+dFTbHxahh6sW6ftXv04v5Hghq08IMMiCfLlorKnmmJEiHFKBi0Jy X-Received: by 2002:a17:906:1615:: with SMTP id m21mr7671181ejd.279.1630033901076; Thu, 26 Aug 2021 20:11:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630033901; cv=none; d=google.com; s=arc-20160816; b=C63T48BJFAMdllSpy4MZm+7ShmtlofHrTktmFZJSyiCJT6a0TWK5XQFS0D7390im1T N+lHm9Qd664v53qhmgV6ubEGYCgJKTRTtT9joUWHxMleLAZ7s9MCEfFfRbVcLqLZcZXM 3zRaGujD1KdrhLySeXZOpUsDtt+oeFGAvIG98tUY8iLhLzRYVNwdF8AxgOKdSmT24Qt2 /5U0ts0fvosfy317O8NEFrgGTMQPDzolYzi3yNE/CZNBeoFk778cKk8FQBV5xBDHJgWP NAAFI3lPRP8CGowJ024VsGDSUijnZFy7py7skOKF2LHn7z7oIgEe1wKO+vfZ8PKsmh3N 9vTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=A0T8uTR7GMZqtBCbDn/gFQIDHCnH77G7fVAFcMza5dc=; b=BpnqoInru6QH6XNMpONvp2GX9MYy4U1YLR3C1pRKQxvDa74p/4sGMgoly6IrRIejfk jurAgOdnc6rXTDuzLXTqBxxgz86eOLzLxyDuJLUz4aJpOnXIsaTlWReQWqwM3LW9LZHW oYfdqPaIkZ7yye0OY23jrhXEdsDRYzBPu5gGIuXLDRq405ieIkOi2F78ubw5W6p7jMFY Pos7mKWk3K/pkOxqJb1kO2c4zIVHY6JpdSt5myeT1Wh2UmmWSPX1MjGmUFuxiVMqM8f2 vGoLFHtOcpAsWZBUHDq59KWJDtWjOJb4VmwWxRWd12Ek/dKa3iiXqG9z61bEE/vfm0ME RM6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RTQNLhPE; 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 h15si4936533edv.59.2021.08.26.20.11.17; Thu, 26 Aug 2021 20:11:41 -0700 (PDT) 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=RTQNLhPE; 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 S244004AbhH0DKw (ORCPT + 99 others); Thu, 26 Aug 2021 23:10:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232208AbhH0DKq (ORCPT ); Thu, 26 Aug 2021 23:10:46 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FE3BC061757 for ; Thu, 26 Aug 2021 20:09:58 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id d17so3036539plr.12 for ; Thu, 26 Aug 2021 20:09:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=A0T8uTR7GMZqtBCbDn/gFQIDHCnH77G7fVAFcMza5dc=; b=RTQNLhPEzWsh2Lauk0HYElABZJ+QsERiqC/cM4gadfwqV3fVLLURSgxMXdzSiz26bS IVaFUx0bR6tT2+oWog+ObpVTHcRR4583kYQi1NAdU1KTWzdV5np1thbramOM6615/40v TzLoELgsoc1qBqeya7L0yB2ULv0wvSgeF7Qls= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=A0T8uTR7GMZqtBCbDn/gFQIDHCnH77G7fVAFcMza5dc=; b=dxjUc301SDGfJihGm2X/AyEU0pd8VyiCv+ty80X6+QRklS5oOPh+DyBsRdOoPk19N7 JAKCT6Jp+/h88oHgpC++CRB3AgXxLMEYCtUoXNrhMnurROePcT422dIB81fdNRsIGo09 gCda5s+u2GUpPvwLX2bpAAu27IrjbyAD7/kOv9l6LPqV8qrfRjv35GkAu9YfxTDL1Jfh JKD7R3lmV9qfia1sfT5hdhERL9ULqdDrAG61aMA9/svRs5c0NjTtTyLr3c2SBrX9YeGS SNQS3oL0td17O9E646Qgrbelfhv5UanaxOmvyBw1BQ8DjMIvNlFbk2Af3gvdb7TzpAP8 1zag== X-Gm-Message-State: AOAM531iMjPbIBt6+6CGGzV19C1HFrpl7p+AEYLziBfrkNKhR9yYqpJD QUVaCQix25+wCzqnYLuw3ZTzHQ== X-Received: by 2002:a17:90a:d589:: with SMTP id v9mr20307795pju.61.1630033797527; Thu, 26 Aug 2021 20:09:57 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c124sm4358304pfc.216.2021.08.26.20.09.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Aug 2021 20:09:56 -0700 (PDT) Date: Thu, 26 Aug 2021 20:09:55 -0700 From: Kees Cook To: Alex Deucher Cc: Christian =?iso-8859-1?Q?K=F6nig?= , "Pan, Xinhui" , David Airlie , Daniel Vetter , Hawking Zhang , Feifei Xu , Likun Gao , Jiawei Gu , Evan Quan , amd-gfx list , Maling list - DRI developers , Lijo Lazar , Alex Deucher , Andrey Grodzovsky , Luben Tuikov , Sathishkumar S , Jonathan Kim , Darren Powell , Huang Rui , Xiaojian Du , Ryan Taylor , Graham Sider , Kevin Wang , David M Nieto , Lee Jones , John Clements , LKML , linux-hardening@vger.kernel.org Subject: Re: [PATCH v2] drm/amd/pm: And destination bounds checking to struct copy Message-ID: <202108262009.85E4C2B53@keescook> References: <20210825161957.3904130-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 26, 2021 at 03:51:29PM -0400, Alex Deucher wrote: > On Wed, Aug 25, 2021 at 12:20 PM Kees Cook wrote: > > > > In preparation for FORTIFY_SOURCE performing compile-time and run-time > > field bounds checking for memcpy(), memmove(), and memset(), avoid > > intentionally writing across neighboring fields. > > > > The "Board Parameters" members of the structs: > > struct atom_smc_dpm_info_v4_5 > > struct atom_smc_dpm_info_v4_6 > > struct atom_smc_dpm_info_v4_7 > > struct atom_smc_dpm_info_v4_10 > > are written to the corresponding members of the corresponding PPTable_t > > variables, but they lack destination size bounds checking, which means > > the compiler cannot verify at compile time that this is an intended and > > safe memcpy(). > > > > Since the header files are effectively immutable[1] and a struct_group() > > cannot be used, nor a common struct referenced by both sides of the > > memcpy() arguments, add a new helper, amdgpu_memcpy_trailing(), to > > perform the bounds checking at compile time. Replace the open-coded > > memcpy()s with amdgpu_memcpy_trailing() which includes enough context > > for the bounds checking. > > > > "objdump -d" shows no object code changes. > > > > [1] https://lore.kernel.org/lkml/e56aad3c-a06f-da07-f491-a894a570d78f@amd.com > > > > Cc: "Christian K?nig" > > Cc: "Pan, Xinhui" > > Cc: David Airlie > > Cc: Daniel Vetter > > Cc: Hawking Zhang > > Cc: Feifei Xu > > Cc: Likun Gao > > Cc: Jiawei Gu > > Cc: Evan Quan > > Cc: amd-gfx@lists.freedesktop.org > > Cc: dri-devel@lists.freedesktop.org > > Reviewed-by: Lijo Lazar > > Acked-by: Alex Deucher > > Signed-off-by: Kees Cook > > --- > > v2: > > - rename and move helper to drivers/gpu/drm/amd/pm/inc/amdgpu_smu.h > > - add reviews/acks > > v1: https://lore.kernel.org/lkml/20210819201441.3545027-1-keescook@chromium.org/ > > --- > > drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 + > > drivers/gpu/drm/amd/pm/inc/amdgpu_smu.h | 24 +++++++++++++++++++ > > .../gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c | 6 ++--- > > .../gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c | 8 +++---- > > .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c | 5 ++-- > > 5 files changed, 33 insertions(+), 11 deletions(-) > > > > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h > > index dc3c6b3a00e5..c911387045e2 100644 > > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h > > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h > > @@ -1452,4 +1452,5 @@ static inline int amdgpu_in_reset(struct amdgpu_device *adev) > > { > > return atomic_read(&adev->in_gpu_reset); > > } > > + > > #endif > > diff --git a/drivers/gpu/drm/amd/pm/inc/amdgpu_smu.h b/drivers/gpu/drm/amd/pm/inc/amdgpu_smu.h > > index 715b4225f5ee..29031eb11d39 100644 > > --- a/drivers/gpu/drm/amd/pm/inc/amdgpu_smu.h > > +++ b/drivers/gpu/drm/amd/pm/inc/amdgpu_smu.h > > @@ -1335,6 +1335,30 @@ enum smu_cmn2asic_mapping_type { > > #define WORKLOAD_MAP(profile, workload) \ > > [profile] = {1, (workload)} > > > > +/** > > + * amdgpu_memcpy_trailing - Copy the end of one structure into the middle of another > > + * > > + * @dst: Pointer to destination struct > > + * @first_dst_member: The member name in @dst where the overwrite begins > > + * @last_dst_member: The member name in @dst where the overwrite ends after > > + * @src: Pointer to the source struct > > + * @first_src_member: The member name in @src where the copy begins > > + * > > + */ > > +#define amdgpu_memcpy_trailing(dst, first_dst_member, last_dst_member, \ > > I would change this to smu_memcpy_trailing() for consistency. Other Sure; I will send a v3. > than that, it the patch looks good to me. Did you want me to pick > this up or do you want to keep this with the other patches in your > series? Since this has no external dependencies, it's probably best to go via your tree. Thanks! -Kees > > Thanks! > > Alex > > > + src, first_src_member) \ > > +({ \ > > + size_t __src_offset = offsetof(typeof(*(src)), first_src_member); \ > > + size_t __src_size = sizeof(*(src)) - __src_offset; \ > > + size_t __dst_offset = offsetof(typeof(*(dst)), first_dst_member); \ > > + size_t __dst_size = offsetofend(typeof(*(dst)), last_dst_member) - \ > > + __dst_offset; \ > > + BUILD_BUG_ON(__src_size != __dst_size); \ > > + __builtin_memcpy((u8 *)(dst) + __dst_offset, \ > > + (u8 *)(src) + __src_offset, \ > > + __dst_size); \ > > +}) > > + > > #if !defined(SWSMU_CODE_LAYER_L2) && !defined(SWSMU_CODE_LAYER_L3) && !defined(SWSMU_CODE_LAYER_L4) > > int smu_get_power_limit(void *handle, > > uint32_t *limit, > > diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c b/drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c > > index 273df66cac14..bda8fc12c91f 100644 > > --- a/drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c > > +++ b/drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c > > @@ -483,10 +483,8 @@ static int arcturus_append_powerplay_table(struct smu_context *smu) > > > > if ((smc_dpm_table->table_header.format_revision == 4) && > > (smc_dpm_table->table_header.content_revision == 6)) > > - memcpy(&smc_pptable->MaxVoltageStepGfx, > > - &smc_dpm_table->maxvoltagestepgfx, > > - sizeof(*smc_dpm_table) - offsetof(struct atom_smc_dpm_info_v4_6, maxvoltagestepgfx)); > > - > > + amdgpu_memcpy_trailing(smc_pptable, MaxVoltageStepGfx, BoardReserved, > > + smc_dpm_table, maxvoltagestepgfx); > > return 0; > > } > > > > diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c b/drivers/gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c > > index f96681700c41..88a4a2aed48e 100644 > > --- a/drivers/gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c > > +++ b/drivers/gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c > > @@ -431,16 +431,16 @@ static int navi10_append_powerplay_table(struct smu_context *smu) > > > > switch (smc_dpm_table->table_header.content_revision) { > > case 5: /* nv10 and nv14 */ > > - memcpy(smc_pptable->I2cControllers, smc_dpm_table->I2cControllers, > > - sizeof(*smc_dpm_table) - sizeof(smc_dpm_table->table_header)); > > + amdgpu_memcpy_trailing(smc_pptable, I2cControllers, BoardReserved, > > + smc_dpm_table, I2cControllers); > > break; > > case 7: /* nv12 */ > > ret = amdgpu_atombios_get_data_table(adev, index, NULL, NULL, NULL, > > (uint8_t **)&smc_dpm_table_v4_7); > > if (ret) > > return ret; > > - memcpy(smc_pptable->I2cControllers, smc_dpm_table_v4_7->I2cControllers, > > - sizeof(*smc_dpm_table_v4_7) - sizeof(smc_dpm_table_v4_7->table_header)); > > + amdgpu_memcpy_trailing(smc_pptable, I2cControllers, BoardReserved, > > + smc_dpm_table_v4_7, I2cControllers); > > break; > > default: > > dev_err(smu->adev->dev, "smc_dpm_info with unsupported content revision %d!\n", > > diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c b/drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c > > index ec8c30daf31c..d46b892846f6 100644 > > --- a/drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c > > +++ b/drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c > > @@ -409,9 +409,8 @@ static int aldebaran_append_powerplay_table(struct smu_context *smu) > > > > if ((smc_dpm_table->table_header.format_revision == 4) && > > (smc_dpm_table->table_header.content_revision == 10)) > > - memcpy(&smc_pptable->GfxMaxCurrent, > > - &smc_dpm_table->GfxMaxCurrent, > > - sizeof(*smc_dpm_table) - offsetof(struct atom_smc_dpm_info_v4_10, GfxMaxCurrent)); > > + amdgpu_memcpy_trailing(smc_pptable, GfxMaxCurrent, reserved, > > + smc_dpm_table, GfxMaxCurrent); > > return 0; > > } > > > > -- > > 2.30.2 > > -- Kees Cook