Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1925045pxm; Fri, 4 Mar 2022 06:12:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJwGLHJ1sKe2Z92FN/nVXfJT3XckrkDKvuh1YtM+Qlnr6FdtW1drdHP7v1tRMnW/0NMJcYRR X-Received: by 2002:a17:90b:1b12:b0:1bf:1e5d:55ea with SMTP id nu18-20020a17090b1b1200b001bf1e5d55eamr5849118pjb.115.1646403165715; Fri, 04 Mar 2022 06:12:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646403165; cv=none; d=google.com; s=arc-20160816; b=FxbNL25xiYnMejvEFOFOv0nsE7jnw5QKRIZIBYNox7q7O2DFW9VxwLl254RGSp1qu+ ha8BIIAeUAs3IU9gWz1W0wykQpCshRWI9g0r4s+ADgN5CDkaY1ofD5pYLgbX2pky83ug kT7OYM6sNzR8GNEok8/ckRhQkXv9RzQTl+nxVKaknuNVMzOTez71rY6IxtsvDp1mSaRb zRCV34GBvh9moaHJh4cHJw66h8mOammOFfFl3Vkx7ct5NWeQxHA6gTqKurZZUCP/rlNH w4hUU0aW9r2/5aY5saC8I9urn4SqjD5Cl85ymfWug2eHjX6USx9dJWo85nQXzZ7oVYp6 J/nA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Q/M3caOnFE3N93SPkyxpAe4BQV2suFmBpM97xNgq+TA=; b=MPAjuoj1z4jwU/O/hxGYughaFaPG+zvYazjw1oNYomfUx/lBiQavo4yP4GBCfMrTHU +vtf++0A0sQdk82G++nJ/CtrsC7D1emOYmSCaPHoETmGZMpANz6ykyzHG03ZZH0GPqpZ 2KQ7uoZiUzYhJpGwtPLmjt50qXKDwplnGo8t/Z2cTngv2RPvVcheL9JaviuLEhNpiAt/ Zgy7IUKzuEb3rL1iL+ACWe3V4l+EeXg2zcMjZ5bfOpcGv7xVbObNL7aCyOTUWJyFww2F U8HR13Fh8yJ+ny1/I7zATMtwqYwYaRxvJWf/WzP0HEDxkKeT4JponQGq2Y+LUVfydgdW 4xAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="anS/vfnr"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y17-20020a656c11000000b00378c5ee50c1si4476644pgu.464.2022.03.04.06.12.21; Fri, 04 Mar 2022 06:12:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="anS/vfnr"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238138AbiCDHHN (ORCPT + 99 others); Fri, 4 Mar 2022 02:07:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231714AbiCDHHM (ORCPT ); Fri, 4 Mar 2022 02:07:12 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF1F718E430; Thu, 3 Mar 2022 23:06:25 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id b8so6647606pjb.4; Thu, 03 Mar 2022 23:06:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q/M3caOnFE3N93SPkyxpAe4BQV2suFmBpM97xNgq+TA=; b=anS/vfnrEbn89xVNCCdRDdOukM3FAdc/3eOHFcE9uWJA+9Xx46X3WP0tLapUtRkGPd Hj0FMecC2bNJmBBnyKEJqIOuvkWrTVI8vwTS6oXYbo6c38lu70/HoAnkMHCPSW8k3rOh ekyJ6fFjyp61UiOGj1uhC5ZoYVdioNtO9AxJmFzumb1AY5ffwwLjcPWPbsV8s2wZJq4h 7ZZ39NvWbSpJezJ95Xm6u7Nsh4kL8sEWvDwuU73cBWIET6ZJ1iYihl6SoP5bf146yDJG gtYKBwgDcbRy3qfT8QQhzxWbHuOpuB76lauxlTn7F2YXaeeWkHg1FAAWjenCOdF7bk89 3a3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q/M3caOnFE3N93SPkyxpAe4BQV2suFmBpM97xNgq+TA=; b=jbKdqX+xh8BQVrnKSWgI64PQwZYC5l0A3mh095Sii9AhHvrfeRFAhCfcln3UPpNnyA Gc1FOnxniPFYwU5ZOFcUoANOaItY6M7v2r+0D3gMXxZlCSLW6DpFpgT3+aDwGX54852x 3E1YJi2Au8sApALefw9VYOYNeQOp2uh1kMisgD/orG3+iKAEEhM6WiXkejWRb4oi4Ulf aqx0BDkKljHuDgLhLy+TqP2SPsKHBWKqJNuPMEeLj4DlhW0ssFsWn3MgPw9P9UlHJcsy JucdVDtJQzgImknTJiucHWJaVF0pvi7eK4RVKIKioOP5mbFXwoGgC+k6fqK6a1gZJ7r2 k+aw== X-Gm-Message-State: AOAM5338RmWVKUxrTOA3DLideliS0IcMp95pobaE+hO0Qym8d3xQVqjV jmqQtKE8noqpYq52dwLgxB8= X-Received: by 2002:a17:902:e5cf:b0:151:b24e:8d3b with SMTP id u15-20020a170902e5cf00b00151b24e8d3bmr4883162plf.29.1646377585360; Thu, 03 Mar 2022 23:06:25 -0800 (PST) Received: from localhost.localdomain ([223.212.58.71]) by smtp.gmail.com with ESMTPSA id c34-20020a630d22000000b0034cb89e4695sm3783586pgl.28.2022.03.03.23.06.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 23:06:24 -0800 (PST) From: Yuntao Wang To: yhs@fb.com Cc: andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, kafai@fb.com, kpsingh@kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, songliubraving@fb.com, ytcoode@gmail.com Subject: [PATCH bpf-next v2] bpf: Replace strncpy() with strscpy() Date: Fri, 4 Mar 2022 15:04:08 +0800 Message-Id: <20220304070408.233658-1-ytcoode@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Using strncpy() on NUL-terminated strings is considered deprecated[1]. Moreover, if the length of 'task->comm' is less than the destination buffer size, strncpy() will NUL-pad the destination buffer, which is a needless performance penalty. Replacing strncpy() with strscpy() fixes all these issues. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings Signed-off-by: Yuntao Wang --- v1 -> v2: replace strncpy() with strscpy() instead of strscpy_pad() kernel/bpf/helpers.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index ae64110a98b5..315053ef6a75 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -225,13 +225,8 @@ BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size) if (unlikely(!task)) goto err_clear; - strncpy(buf, task->comm, size); - - /* Verifier guarantees that size > 0. For task->comm exceeding - * size, guarantee that buf is %NUL-terminated. Unconditionally - * done here to save the size test. - */ - buf[size - 1] = 0; + /* Verifier guarantees that size > 0 */ + strscpy(buf, task->comm, size); return 0; err_clear: memset(buf, 0, size); -- 2.35.1