From patchwork Wed Apr 10 20:43:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13625054 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D69B0181BA7 for ; Wed, 10 Apr 2024 20:43:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712781818; cv=none; b=tWM4fgjaXFzFRqoDameMnUbm2GIwqO1N0TCabPP/npwpcyPbEhimpLcMLYfaDqHGGEFGfRQTwG89Gh/8HBe76wfYAErNv6sPHsv4DUavrscA9yHWM48+MnRKFqrlKNcg/m1UFaMjDC0ANVifxWK+RyfnBS6kveh9eqdiY/kRya8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712781818; c=relaxed/simple; bh=haeE2c1uQMVwuBCh6nmKoLx0syvsO5cganYlWpRKAFo=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=LytSTIXlCZrjJbU94fhJS/UUn5dHQ9ef82jZiecIQ1NnOrFG5Jq1CMvp0aDCBifI8E4zGTzSjGmRjQweInRQFJRoHN0HmXxE+U1eDd/xuQY04zJkt2Csqx/ujs6piTNZtgUdLNRoyeMqCbNGOmOPJijw02h8hiCBz4lHpbdyRDQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DoLxMmlR; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DoLxMmlR" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc746178515so11361295276.2 for ; Wed, 10 Apr 2024 13:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712781816; x=1713386616; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=K4SubXYu8kjsVJfXEh2At6iMxBrwK+80VVQTqnF6NMs=; b=DoLxMmlRxqj9arQZ7eoYfdnWzW8J0AVmixqNjiiG3oQSzMGOioMmkIKAszPVstbEAa /d1pqu+Q+Zcc9iUvLEf7EoST606wod6KCurrvT4gkY1qP9HaqkaukVfz/NbfgccpOrey 37XxLpYfGaMNqVvy4kubX8ofDT6F5f7QkmQwkmrHB02DzFINctIA8dFnGvnC1AfBbXeS RIlmpAkcPVqXKBfPyp29lBncfRqxFrrTtWJM2665utW5mkE9mNbN+plZLcfprZzjGyVj P8qN5xisr9vXjuDUjB/SUVXhDfHjefxSx1ddQqKfFwv5Xgfx+8C8PbweB02cMPnRzLUN fiLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712781816; x=1713386616; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=K4SubXYu8kjsVJfXEh2At6iMxBrwK+80VVQTqnF6NMs=; b=acrJ4w9VogzehLTaEJl4DXV+n7HEnH0xoqw9ttXRKd5DpzFCa8hjtLznnKslvCC9iK 3QqyQioaOPGncpv4UD/oXuXJOXXW0EeFW70qCbCX7ij2M26f9jpbZ309J2CbuQE8TavE kRUKuzt7wUiECZ29omJtb5UmNWnPkgyWQGrDva7k5hMDLcRXAYle3cluEnNwWueWw8xL a+xUSFed0JJ2U/8H7dtYjHVHksb3PHIeZj2F87/iWguF5C+o2QuHnHpUBfo8s0HqTrZ/ kibuPkAaPFuPRHC3frJJytun6gcyB8LzZRprcK/dCMfnSWEC/ubNBqQ7TNbQWC7E66qt AgNA== X-Forwarded-Encrypted: i=1; AJvYcCW7QjbdXFCHUnS8oeebCNAjOPyfl5n5pQ1KS3nIvrTTQhkzcvkBaWB1thi2KxX70gfd0pxH4rNR9k/kIZX/putkVw5qjKzBlaNISZHQ6Wa6 X-Gm-Message-State: AOJu0YyiOJT8k638+Uox0kgfzCSVvpKv4z4TupEjCv3tSxaRby0tzVLA YySuefFLqpWsnRBQuNIVy2tWPQAKHD9PFhXO5d6glp3mUNglBecuGRsZTg5JVh1CozXqmqSrN/M 2CbIVUloYB1dqDtSFXdZZuQ== X-Google-Smtp-Source: AGHT+IG8XPLzn461t7HYg7HcnLuaG3TVHzRMb9P4mUgIRvjW7s/eDccTG/ZbSuOJUAz9pc1Ch9tK3cW5uAl1bxnTTA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:2b83:b0:dd9:1db5:8348 with SMTP id fj3-20020a0569022b8300b00dd91db58348mr1031841ybb.8.1712781815890; Wed, 10 Apr 2024 13:43:35 -0700 (PDT) Date: Wed, 10 Apr 2024 20:43:33 +0000 Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAPX5FmYC/32NQQqDMBBFryKz7pQkRqld9R7FhcZRB6wJGRFFv HtTD9Dle/DfP0AoMgk8swMirSzs5wTmloEbm3kg5C4xGGWssqpAWeLswo5bLyhh4kVjY5xVj7b KrSshDUOknrcr+q4TjyyLj/v1seqf/ZtbNWrMKe96WxZN11avwfthorvzH6jP8/wCoGz2orUAA AA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1712781815; l=2128; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=haeE2c1uQMVwuBCh6nmKoLx0syvsO5cganYlWpRKAFo=; b=MhjGam0PtObrc68tbZTGvY4oh5Wwyx6w16vBGbchHocaRdOCwzZBBtx0op7ozZ3jmt6i7GPf0 AHDzHrYUreVB0COSb1VjQEFFd78V5pwZ7RkSJJY/rJb9V3oI6KkT8tN X-Mailer: b4 0.12.3 Message-ID: <20240410-strncpy-xfs-split1-v2-1-7c651502bcb0@google.com> Subject: [PATCH v2] xfs: replace deprecated strncpy with memtostr_pad From: Justin Stitt To: Chandan Babu R , "Darrick J. Wong" Cc: Kees Cook , linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. sbp->sb_fname may not be NUL-terminated while label is expected to be. memtostr best describes this behavior, specifically, use the pad variant since we're copying out to userspace. 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 --- Changes in v2: - use memtostr_pad (thanks Kees) - Link to v1: https://lore.kernel.org/r/20240405-strncpy-xfs-split1-v1-1-3e3df465adb9@google.com --- Note: This patch relies on the memtostr{_pad} implementation from Kees' patch: https://lore.kernel.org/all/20240410023155.2100422-1-keescook@chromium.org/ Split from https://lore.kernel.org/all/20240401-strncpy-fs-xfs-xfs_ioctl-c-v1-1-02b9feb1989b@google.com/ with feedback from Christoph H. --- fs/xfs/xfs_ioctl.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) --- base-commit: c85af715cac0a951eea97393378e84bb49384734 change-id: 20240405-strncpy-xfs-split1-a2c408b934c6 Best regards, -- Justin Stitt diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c index d0e2cec6210d..7ed7a5d57094 100644 --- a/fs/xfs/xfs_ioctl.c +++ b/fs/xfs/xfs_ioctl.c @@ -1750,15 +1750,14 @@ xfs_ioc_getlabel( char __user *user_label) { struct xfs_sb *sbp = &mp->m_sb; + /* 1 larger than sb_fname, for a NULL byte */ char label[XFSLABEL_MAX + 1]; /* Paranoia */ BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX); - /* 1 larger than sb_fname, so this ensures a trailing NUL char */ - memset(label, 0, sizeof(label)); spin_lock(&mp->m_sb_lock); - strncpy(label, sbp->sb_fname, XFSLABEL_MAX); + memtostr_pad(label, sbp->sb_fname); spin_unlock(&mp->m_sb_lock); if (copy_to_user(user_label, label, sizeof(label)))