From patchwork Mon Oct 16 22:38:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 153790 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3763793vqb; Mon, 16 Oct 2023 15:38:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzgRCHjHbVXisTBD0etXiGgioSAvcol73fP/Le1Ylcn0LsGGsxB5lmwVTRIYLFcCSyXoVc X-Received: by 2002:a05:6a21:8cc5:b0:15c:b7ba:e9ba with SMTP id ta5-20020a056a218cc500b0015cb7bae9bamr397623pzb.0.1697495917459; Mon, 16 Oct 2023 15:38:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697495917; cv=none; d=google.com; s=arc-20160816; b=hXt79TQN15HeLF/75WQqjvPS7IZT7FLKr5lGPep5yDfCb0WQw3woBRAFCukO/B5U/p RU3g5bK//rcB/7ocbLkl77yXRolFqcliETo+nfflyca1SaFkB34WxApy32fPPqlem3vD j6StvD26bH/so32lkfeoPoPxUt+3Z52JSXpp2OJSnb1UvbBuX/CUuK1U8eqE4HABcuzz YVlZJIsVePlaCi0RVW6vcjdtXEZFpEk/3jlU0o3yeGdXUuWGD4eoSTnEii8RmIIM3Nr3 K6CSGM8cbR55f3sDqYcn8ruE5ADNO3KsZCmfk9C/ozBBsVSrJUDRTZHTNDtYMw/oOtVZ XIew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=rfGwHPiYseuIB04WY1jIM0LXVs+wtH1jXMPtU9N43zs=; fh=TzsheIIFV1UI9V+fow+XrbrMeCx9F+rCR14i7B1V+6M=; b=DWLq+jv8hpiPwCVZuTpJKXpK8yJU+6RIst/pw0pc9z95GvTyBezMz/Z1oPqOwN3lrj ZyqzzSPtxkJVX+t92SUnkL6uiTzPbnUmvN1qg1QpyueeINYYpyNwVJmoueGTVR76a8jy iREZTBTLw9M6WXqPkAuy9PtmfTYqaEbaPsKHneCr781gns37f+782KDaXCOOVRnT+8Z8 MfhukUyNJqTNmPsyb3x8WFqmyF3eHryvhygkFM4IJipXyEM02dCi/dfgw5NN9Zq9Ni7E hZMBWHbVN43/BX/PATbUpxToxLXYE5MrrlOp8pVhpTxmI3eRYRAhtbpsZUf/VJEbC+c3 qRZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=J6b3TzD4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id w7-20020a1709029a8700b001c71eed110esi297971plp.254.2023.10.16.15.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 15:38:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=J6b3TzD4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 7688E803957B; Mon, 16 Oct 2023 15:38:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233800AbjJPWi1 (ORCPT + 18 others); Mon, 16 Oct 2023 18:38:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233008AbjJPWiZ (ORCPT ); Mon, 16 Oct 2023 18:38:25 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A79AFE1 for ; Mon, 16 Oct 2023 15:38:23 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a7ba10cb90so74074547b3.3 for ; Mon, 16 Oct 2023 15:38:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697495903; x=1698100703; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=rfGwHPiYseuIB04WY1jIM0LXVs+wtH1jXMPtU9N43zs=; b=J6b3TzD4+gcfJcD2ESwDrx1CSo3G3phEwPdaz22RHEmPLa251ViTgB+XNQnlzCVKSI D0+eUSBFrryylKoc1xtnKobuVxOkQUQMANjgreWwLUybOtmt1kP3xwpjifkCvinUPjVc +XYNcbyTaKxyR28t5NjvkOza/Gq+ne1LsVlklCcQFKEh2suRHRbpZB/wEQa5aEZCj3fh FzorxcmotQSk/2Y1OUuB7QvyqUefqJx7Lq/IbQR+VYwXckczESmQEqMZgjo13NHpKNT1 QwApOwVTcgmyhnlUGGdkPhqgHoBTpWeopU/Ftv9X555iiKlF47GKH7mr2ZLZBHjwI+ie mo6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697495903; x=1698100703; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=rfGwHPiYseuIB04WY1jIM0LXVs+wtH1jXMPtU9N43zs=; b=XalB7BUEwqJbZyFJF+q9b5EHaPhoZk9mhNQzOYK8z1GWI6Kum0TOV2PDFlG8TOYj7T 7KBx1/qKiBMP2ReKAku9HNHQxf/Bu4ANtZDNtaScJZ3UqHn0767ysHyglwtYPLzEauZu d9x04UHeDf+9H7/VXmEhHgVzYWeAYeEddj2srRmXWQEQdhwH6YDJr9OueM1se6dFUMnE 61OLXEp8rJOSPtn4kf0jW6pReP7k4CwYKbSb4bWK/uku12CoM8+VUwzgWoySJqGGakQg RESYwyVLz/zAanLBoM8aGgjIA3lBye1e/FpN1b/IOhpX7BigOFknHJEhJNczfxzBs9Vr /yiw== X-Gm-Message-State: AOJu0Yyi6uhZHOqz8GgDQvz4xDoWoSlzfElqhT4IgGqN2DHRj6phJygj O5voJpOrF8nlcaMKoIdrXoKlApC3Kael7jBLDA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a81:a214:0:b0:5a7:af9a:7530 with SMTP id w20-20020a81a214000000b005a7af9a7530mr13026ywg.1.1697495902914; Mon, 16 Oct 2023 15:38:22 -0700 (PDT) Date: Mon, 16 Oct 2023 22:38:20 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAFu7LWUC/5WNTQqDMBSEryJZ95X8NFi78h5FSpq8xgfVSGJDR bx7ozfoYmC+WXyzsoSRMLFbtbKImRKFsYA8Vcz2ZvQI5AozyaXijbhAmuNopwVcpIwxgZ8+pQ9 7HkNwmMCCUdrVV2mN44IV0xTxRd/j5d4V7inNIS7HaRb7+p8/CxDA6+apldRaGdX6EPwbzzYMr Nu27QepuXgo1wAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697495902; l=2621; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=TJW5gbFFOQpXDfDl5XUoO1+aPZvb/beyqblsIzLDODQ=; b=Jr8r8/EvJkzNSU8MDRFRbOGBeDpanyMebo+SVS5CU/dprVV/kh/VtN1BGAtRJ27wIjeHTlzp1 6/VI8g3xYjjDTFjHLJgbeHe+TZzGUb9EXm/g4j4Wiey/482jTTXVbq/ X-Mailer: b4 0.12.3 Message-ID: <20231016-strncpy-drivers-gpu-drm-drm_modes-c-v2-1-d0b60686e1c6@google.com> Subject: [PATCH v2] drm/modes: replace deprecated strncpy with strscpy_pad From: Justin Stitt To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Xu Panda , Justin Stitt X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 16 Oct 2023 15:38:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779953478778216688 X-GMAIL-MSGID: 1779953478778216688 `strncpy` is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. We should NUL-pad as there are full struct copies happening in places: | struct drm_mode_modeinfo umode; | | ... | struct drm_property_blob *blob; | | drm_mode_convert_to_umode(&umode, mode); | blob = drm_property_create_blob(crtc->dev, | sizeof(umode), &umode); A suitable replacement is `strscpy_pad` due to the fact that it guarantees both NUL-termination and NUL-padding on the destination buffer. Additionally, replace size macro `DRM_DISPLAY_MODE_LEN` with sizeof() to more directly tie the maximum buffer size to the destination buffer: | struct drm_display_mode { | ... | char name[DRM_DISPLAY_MODE_LEN]; Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Cc: Xu Panda Signed-off-by: Justin Stitt Reviewed-by: Kees Cook --- Changes in v2: - use strscpy_pad (thanks Kees) - rebase onto mainline - Link to v1: https://lore.kernel.org/r/20230914-strncpy-drivers-gpu-drm-drm_modes-c-v1-1-079b532553a3@google.com --- Note: build-tested only. --- drivers/gpu/drm/drm_modes.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) --- base-commit: 58720809f52779dc0f08e53e54b014209d13eebb change-id: 20230914-strncpy-drivers-gpu-drm-drm_modes-c-a35d782cad01 Best regards, -- Justin Stitt diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c index ac9a406250c5..893f52ee4926 100644 --- a/drivers/gpu/drm/drm_modes.c +++ b/drivers/gpu/drm/drm_modes.c @@ -2617,8 +2617,7 @@ void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out, break; } - strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); - out->name[DRM_DISPLAY_MODE_LEN-1] = 0; + strscpy_pad(out->name, in->name, sizeof(out->name)); } /** @@ -2659,8 +2658,7 @@ int drm_mode_convert_umode(struct drm_device *dev, * useful for the kernel->userspace direction anyway. */ out->type = in->type & DRM_MODE_TYPE_ALL; - strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); - out->name[DRM_DISPLAY_MODE_LEN-1] = 0; + strscpy_pad(out->name, in->name, sizeof(out->name)); /* Clearing picture aspect ratio bits from out flags, * as the aspect-ratio information is not stored in