From patchwork Tue Oct 3 21:01:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 148057 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp2349215vqb; Tue, 3 Oct 2023 14:02:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExUpFQmVoGUhWHuAcBJ5xJyGAlmqslj1kVhsyslx7G3MUADk4VjwjGJ21swMl4nVnblS7q X-Received: by 2002:a05:6358:7252:b0:134:c859:d32a with SMTP id i18-20020a056358725200b00134c859d32amr601622rwa.25.1696366950760; Tue, 03 Oct 2023 14:02:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696366950; cv=none; d=google.com; s=arc-20160816; b=BLsgoiJuE78CJB0F2iJRtKAOTFpYAjiQDZVB8zN+1woZO5u1eQKvuWarg17UC75YZJ gpk5dFzcagUpXe2FOe4UCkxL0SjgOj6T06hQ/oWBDIece1YuP6WeKFmsuC8j5qoZI8gr 7GPChuJyRcq+wpawVL8aLwZu4JUbZtR7LZm+INXhIREzuo8xtLDiRPKQhSKWB22l3DcB Ykr9Y6ByuTtXoqwcsa9BvkCuzwv0HCw9TWOtksWwMiMUVYRzko2x/lTel8KiN9j+Pybs f84u187+CbT6Nz+fDDtFoOnh1alUM6Gxu6eywLcoKUTM+p9UVR7QBXUa15vBuWZxTKvI f/dw== 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=9OlLlHc2AYiWsThRdc3PqfHZGtUph4N7so6saZ8WNZY=; fh=rfLEqjYI8PPB4dcST09VoFi1ySLkgpJKcPUXZkzsebk=; b=fba+U1dwDhpsRiKt3J05M9L9L4huj83lNosIXgNeUA2bC5KBbluilrF7t6PRH+c/sp IX+Tp+yfBnUafnIiJZ1aF7S1BG38behj0m1BhAr9doHE8bKRBRij4hSNxiRPDt93iWLa BaiHtI/GmppazLLm4eBHzNjdvIGkc6NvjqOOSNPP8Wch72+C2RmTzRB2N5ddgSvbzl6H XSYrApuiWuoum7yy8QXXYYO7jvavQte9YxM07N+c5BNB6E6TRZ/H4VjTky4LAtnZMmE+ g5ChHdbSYInhV/m0WMKeQZ1Y9lQRBJlcKRmNSZtOJ4rf9/pdPKFBRBykDOZ3ZO8VCApb NIoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ciA6jMiV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id l63-20020a638842000000b00578c3b7af9fsi2225454pgd.184.2023.10.03.14.02.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 14:02:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ciA6jMiV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 1E2C281829EF; Tue, 3 Oct 2023 14:02:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241104AbjJCVCH (ORCPT + 17 others); Tue, 3 Oct 2023 17:02:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231195AbjJCVCG (ORCPT ); Tue, 3 Oct 2023 17:02:06 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44EBAB0 for ; Tue, 3 Oct 2023 14:02:02 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d868842eda1so1814893276.0 for ; Tue, 03 Oct 2023 14:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696366921; x=1696971721; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=9OlLlHc2AYiWsThRdc3PqfHZGtUph4N7so6saZ8WNZY=; b=ciA6jMiVZFrxHCY0rquKorgXufiRCKFG+cQDCTz4GBxmMwnxBGra5kD71Lyua9/LEi 6Vji2YdIcbe2T3YpCqlgBjKdohwq06Np+VeUXKZCA7d4ZXUvuYkO8jSRM/KRQI+dk+K3 0k8LGAC+4Ploy6UYT9q5msGBCcgHdyKcPcYxDRFbeEzcRVKbcpGCoXDuEu6BvDBn+2JF ppXmGYR5cJQjzvutGY07sWIW5jRA52qLMuB0Fk3Q9dnG5FlqUbj9MeoV/ybTd3sc6gDv eOO3TjpMd9mpOmo69LIfZWGppkvhsfEi5LeKDTz6VEwo4HkolvKbB2pihoJeDxkMHdqq oy5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696366921; x=1696971721; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9OlLlHc2AYiWsThRdc3PqfHZGtUph4N7so6saZ8WNZY=; b=rbX2CHjG71qeCRmKiEx0Pvrnt9FcyE77jpfYqH+MekMMR3SeK4lAaFuN/JrzgHs7cj bK+IsTHrv65gAEV3d+x95mu1Rei1RVLAORpXaAJAPc0eNytdmFd8uzabBiu4SKy2gphr GBEGxMvIo7itCRJQ8jXZrYmk8HIoCeipW3AsMBnOtPWsSalLWc3pkuvQoMmJlEUsUb+w LEvzHILdVBYrW2CGUspvy3C0/M5qK1a5XKmPbfzO1tn/sVQdAaLFZ7XXECgrukf3oNci ZPZ57Hgjlp8Fh3qRZh/Q9efaI3Ve52DA/vTNSJoyoPNQaS3y+AWnF4VFtrYGfobPVHMD MkAQ== X-Gm-Message-State: AOJu0Yzz0ij6GQ/r+FD9HdPKrC5+UicCUX9szXEtM9nCL/xCyar8/YkL IWz4SRh1raHs1RgL/PwCMNW7pgrTjxp2pV7EHw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:212:b0:d89:b072:d06f with SMTP id j18-20020a056902021200b00d89b072d06fmr6867ybs.7.1696366921501; Tue, 03 Oct 2023 14:02:01 -0700 (PDT) Date: Tue, 03 Oct 2023 21:01:58 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAEWBHGUC/42NQQqDMBBFryKz7pRErbVd9R7FhWRGHWiNTGyoi Hdv9ATlw4f3F++vEFiFA9yzFZSjBPFjgvyUgRvasWcUSgy5yQtzsyWGWUc3LUgqkTXgIISfvRy 2ZXXpCr7WJREkwaTcyfeQP5vEg4TZ63J8Rbuvf2mjxZS6tTdjKkM1PXrv+xefnX9Ds23bD8box cjFAAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1696366920; l=2843; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=fPTrIY2uQ734r/+gbDBG2QG/Vp0F9Llz6laPStW03L4=; b=ix0M1cN1JC7FZ8ld1+Vt91xRDUNU/CZiyhxc6Hf8g7BcWGtYE678ITzZyYtAutza+nAvpgGHn s6qhBc5/ZwzB9TsSz7H51MzEGlkeJtCYd7EQ/PpJ8q9tWFPBbpbOjWN X-Mailer: b4 0.12.3 Message-ID: <20231003-strncpy-drivers-hid-uhid-c-v2-1-6a501402581e@google.com> Subject: [PATCH v2] HID: uhid: replace deprecated strncpy with strscpy From: Justin Stitt To: David Rheinsberg , Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, Kees Cook , linux-kernel@vger.kernel.org, David Rheinsberg , linux-hardening@vger.kernel.org, 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 pete.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 (pete.vger.email [0.0.0.0]); Tue, 03 Oct 2023 14:02:28 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778769671410406680 X-GMAIL-MSGID: 1778769671410406680 `strncpy` is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. A suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on the destination buffer without unnecessarily NUL-padding. Furthermore, let's make sure `hid->xyz` and `ev->u.create2.xyz` are the same size at compile time to prevent silent truncation. With these changes, it is abundantly clear what the intent and behavior of the code is -- We are getting a string to string copy with NUL-termination and no truncation. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Cc: Kees Cook Signed-off-by: Justin Stitt Reviewed-by: Kees Cook --- Changes in v2: - update subject + commit message (thanks Kees) - use BUILD_BUG_ON size mismatch (thanks Kees and David) - Link to v1: https://lore.kernel.org/r/20230914-strncpy-drivers-hid-uhid-c-v1-1-18a190060d8d@google.com --- drivers/hid/uhid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) --- base-commit: 3669558bdf354cd352be955ef2764cde6a9bf5ec change-id: 20230914-strncpy-drivers-hid-uhid-c-a465f3e784dd Best regards, -- Justin Stitt diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c index 4588d2cd4ea4..a54c7995b9be 100644 --- a/drivers/hid/uhid.c +++ b/drivers/hid/uhid.c @@ -490,7 +490,7 @@ static int uhid_dev_create2(struct uhid_device *uhid, const struct uhid_event *ev) { struct hid_device *hid; - size_t rd_size, len; + size_t rd_size; void *rd_data; int ret; @@ -514,13 +514,12 @@ static int uhid_dev_create2(struct uhid_device *uhid, goto err_free; } - /* @hid is zero-initialized, strncpy() is correct, strlcpy() not */ - len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1; - strncpy(hid->name, ev->u.create2.name, len); - len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1; - strncpy(hid->phys, ev->u.create2.phys, len); - len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1; - strncpy(hid->uniq, ev->u.create2.uniq, len); + BUILD_BUG_ON(sizeof(hid->name) != sizeof(ev->u.create2.name)); + strscpy(hid->name, ev->u.create2.name, sizeof(hid->name)); + BUILD_BUG_ON(sizeof(hid->phys) != sizeof(ev->u.create2.phys)); + strscpy(hid->phys, ev->u.create2.phys, sizeof(hid->phys)); + BUILD_BUG_ON(sizeof(hid->uniq) != sizeof(ev->u.create2.uniq)); + strscpy(hid->uniq, ev->u.create2.uniq, sizeof(hid->uniq)); hid->ll_driver = &uhid_hid_driver; hid->bus = ev->u.create2.bus;