From patchwork Thu Mar 21 00:10:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13598285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F110C54E58 for ; Thu, 21 Mar 2024 00:11:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8BF66B0083; Wed, 20 Mar 2024 20:11:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B14C06B0085; Wed, 20 Mar 2024 20:11:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 966F26B0087; Wed, 20 Mar 2024 20:11:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7EAC36B0083 for ; Wed, 20 Mar 2024 20:11:03 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2342EC1437 for ; Thu, 21 Mar 2024 00:11:03 +0000 (UTC) X-FDA: 81919116006.28.55DC39F Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 4ED50120011 for ; Thu, 21 Mar 2024 00:11:01 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qEVCVkdK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3FHv7ZQsKCC8UfdeTYdeTeeRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--justinstitt.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3FHv7ZQsKCC8UfdeTYdeTeeRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--justinstitt.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710979861; h=from:from:sender: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:in-reply-to: references:dkim-signature; bh=+Y/++eaKr1uF0F/QMSpfiG9X2bG1rR6plkUxL7VYSLI=; b=6NooPnC6Nys68V82E94R9GbF9nc7T8z7y0SlMPsw6Hfs5uLrzsdhT2nfDXYZQA8rUjrYXA DtgJKI/DpRty2jOP72F4a312Ro9PO13hEbIXJXfkLAOpoqJzYT63nJ3EfpOib3Z08Jekp3 Pna6Ji5sGCehad2gujAEX5z0MGBERKg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qEVCVkdK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3FHv7ZQsKCC8UfdeTYdeTeeRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--justinstitt.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3FHv7ZQsKCC8UfdeTYdeTeeRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--justinstitt.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710979861; a=rsa-sha256; cv=none; b=B6AtNqMWRMsNszZqLRfG02UcnFdmsew5+98WCuYJ3w64zumyDYTaxEyBopIRiBI9iXcDfu RJ9P13xy2fz0Z0LbZ9GsJJ4qKTcmHacXm/+W+BklHxy3mZUe3POigmGbje7CmEa3R2J6Ql tqgO+wBXj0vuEukdnNe7Sk4MZ3GmbV4= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dce775fa8adso668540276.1 for ; Wed, 20 Mar 2024 17:11:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710979860; x=1711584660; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=+Y/++eaKr1uF0F/QMSpfiG9X2bG1rR6plkUxL7VYSLI=; b=qEVCVkdKibk8ThIR2CzfiUapNkVtwZ1cuDaXW4WpkyfsSpPWN+tKZkt31fbNVI4YaU L/oa610jY6egw7ZLWpB5pOm53F8x8opaEkH8Q5g70HfsJFPLxSy48st3d39pPftV3IP/ /LEiX7cGg7FiWnQ4erHCC91/Xw+33+5vRLQDsVjnHj4ncFwkyWiWpTrnZK8CSsTn5C5A THVSeLX+gDaPL9YgbEiS3yby7GD21nV5IhAWXNmoZYiEdhw1VPi6KYK3QDVjQ9LXVohT nzGi6co+fHG9ENUNdMPZw4zHPALlX4R0Az6B+H4z1yfcBC60cxLiz6LiVamFD8ORCoIz OiOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710979860; x=1711584660; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+Y/++eaKr1uF0F/QMSpfiG9X2bG1rR6plkUxL7VYSLI=; b=miz18evsKnuONEcj3/lVgpII9sK1MzsClpSXw8Rd9EfYLsufY3ZbNMQpE+unjcwV6/ 90OOHORGV5fvXwzzOXncjrxsAv10ltuUp9l9QNRaM7Alh+nOWTl5VqMjxoMcGbxGQ9b8 rDOQxc3JTsHJins+ZZN1+zoI4p1mMZ+2ZXNEgbCGN7KxxhZcvwGWysZyTOTN8ADZwRTT 0w52mOntx5v77nWY+uxoL1Hs3QnkOPMW0mF3WPwZWmn1iEOvK/GASExvacjPRKYIGKs8 2vLZKaYFJd9LXQ2F3TCsaRhBG6VlXYug/3fdtXNtonxy4XTnBwv/9cbSsqQrA0ZyGRQu TGlQ== X-Forwarded-Encrypted: i=1; AJvYcCWyZfBmH6pdWoMMF4O1IWs4woFqclk5I0SY4gGIZVxv8z1iq9h+QSZXnvAuFV02PhRzL+b191uK9Zyz4zLEFvcTeQo= X-Gm-Message-State: AOJu0YzEcSvo7ZOkc/bfaSsPOWJrKC45TYNGXSvFlCIaOAbkqF8TKP44 f1D3GoMQJQDYCf1KuFjfbpKgIf/sCD4uOzMcOSSv2F7eJ0GXntyowP98CSVQJHi4ZaZ8bNRo0S8 LSZLWPBX29Mj/d9Xp/AT2BA== X-Google-Smtp-Source: AGHT+IGf505ZVazw6jjUV6V9zvF2/vHvzzK6R5KLbh0GdIFxY2g4V4MVPW/wlAhrkwOVCVplbt5u8DMwwFADJQzvxg== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:1b91:b0:dcc:6065:2b3d with SMTP id ei17-20020a0569021b9100b00dcc60652b3dmr4933165ybb.8.1710979860466; Wed, 20 Mar 2024 17:11:00 -0700 (PDT) Date: Thu, 21 Mar 2024 00:10:59 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIABN7+2UC/x3MQQqDMBBA0avIrB2IUaz0KiLSJjN2wMaQCVIR7 94gf/U2/wSlJKTwrE5ItIvKFgqaugL3eYWFUHwxWGM701qDmlNw8UBWfEvgb55p5Zl9FIcOB1v q/aNvGwPlEROx/O7/OF3XHyjMQ/hvAAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1710979859; l=1970; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=qLFMjKBHWBbdNmXG9Gp9YCIsExWfJWYt/CixfHOVoOs=; b=Y9r+dN0unQvQHHcwvbxTa+ghrDkz84FfNC4ehnh5o5e+cMt0sywA8r4Lzk0cBi+LdcSfrlgRj +FLMm+BPNOZBHvfDKNPhK/wDxJMzw04FfOepyTH+/VSwi9grFbpRaRm X-Mailer: b4 0.12.3 Message-ID: <20240321-strncpy-fs-binfmt_elf_fdpic-c-v1-1-fdde26c8989e@google.com> Subject: [PATCH] binfmt: replace deprecated strncpy with strscpy_pad From: Justin Stitt To: Alexander Viro , Christian Brauner , Jan Kara , Eric Biederman , Kees Cook Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt X-Rspamd-Queue-Id: 4ED50120011 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 8q7f955en17aqrqrjy7fkjg3kgwskgae X-HE-Tag: 1710979861-921953 X-HE-Meta: U2FsdGVkX18h7eDsz4NRsIv0i3v4+Xut030R0T/ankFnfO5iCOcdqkujWDYx80zvKrp0cl4PPFHaC+zpsPMWCHCw3VhqLdyhkYxTj0B2ufDSz0DOYUwoIWVXRK/JP7X1nK+RdlRBQe4IWVRJRaOnJploIJuepA6VYa/uCrUuApvJpSniQvc0vZO+H/6WPIQHRlJodXQk3xdbswZ8CZK6pdQRFkjfLsXe5YZFC1EQgy/OASahJ9zzd3ifzhWngBP/bPLEFzIZjcF7NWt29T6Mxut1OTescssSWSoLRuOE0In0eHQyiNFpJlRvrTR35hKfA0ntuJVz6x5T6g9mxe6cIc6ApgHHVk5LM19gQt8eejTAfSgMgQ5oiCQJ0dJctdKrdUrM90e3V7q7Io0QmKj5LSwrxumEDe4AB1tEyE5hZ0uE+wEc5ag6pNf5eUauKUu/oo+uFi1o74VMPheGcpzZmGxLtsl1FDoD2kNZNfYiP3knXhyjaq+k941p7Ymih2j+A5ZJVI3J3C15UGq8Ty2rTbLBOZZyUgHeCU2eM9lOsAwT3l4OvWlzAOjuA9tJTNUi3t2MKWjqgPqrtrsrGI/2OU38/ht1nJU88cRzmux5oF+pHlZ7c/ECn0YIafklDSiFE840w2hY11AQNkJWQw/uO8M7wTTXdrnmpc7jXx6192FixcKyu5pGncy9av8T6PVAhEAn0jL9aqvLps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3MY8za6t/4hJUILgV6uG70hl2ZylHIP+RPHXKPAPlqAye8A+ryZeFMKbcGbXjTCMawdURiJTWN5Iu+nwGsqHviiFphRwMECB/IeJVDCvN+HJafgHK+guIthYJP X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. In every other location psinfo->pr_fname is used, it's with strscpy_pad. It's clear that this field needs to be NUL-terminated and potentially NUL-padded as well. binfmt_elf.c +1545: | char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) | { | task_lock(tsk); | /* Always NUL terminated and zero-padded */ | strscpy_pad(buf, tsk->comm, buf_size); | task_unlock(tsk); | return buf; | } Note that this patch relies on the _new_ 2-argument versions of strscpy() and strscpy_pad() introduced in Commit e6584c3964f2f ("string: Allow 2-argument strscpy()"). 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 Signed-off-by: Justin Stitt --- Note: build-tested only. Found with: $ rg "strncpy\(" --- fs/binfmt_elf_fdpic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- base-commit: a4145ce1e7bc247fd6f2846e8699473448717b37 change-id: 20240320-strncpy-fs-binfmt_elf_fdpic-c-828286d76310 Best regards, -- Justin Stitt diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c index 1920ed69279b..0365f14f18fc 100644 --- a/fs/binfmt_elf_fdpic.c +++ b/fs/binfmt_elf_fdpic.c @@ -1359,7 +1359,7 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p, SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid)); SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid)); rcu_read_unlock(); - strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname)); + strscpy_pad(psinfo->pr_fname, p->comm); return 0; }