Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1606214lqz; Mon, 1 Apr 2024 11:11:04 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUjzASGlTVR2SClBnBKNCLfLj8UUleGt8Cx2e88qzmaoc5xyYk3DwPdwsRC7LtDqV3k+ZfVjXXIGBkIZ9BOVtURk/ZI2gQI8LfetQ4Yyg== X-Google-Smtp-Source: AGHT+IH2KjsKkQP2BIbQMUo3WDqYO9kcXDWkmF9Ycck9lLLyTJXoe1OANNO2fqd5G9o3HzNASyrZ X-Received: by 2002:a05:6402:40c1:b0:56b:c4ad:ae29 with SMTP id z1-20020a05640240c100b0056bc4adae29mr7771672edb.3.1711995064555; Mon, 01 Apr 2024 11:11:04 -0700 (PDT) Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d24-20020a056402401800b005657c432df5si4850427eda.16.2024.04.01.11.11.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 11:11:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-126935-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20230601 header.b=rHMkJeSL; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-126935-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126935-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=REJECT sp=REJECT dis=QUARANTINE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 225631F226CD for ; Mon, 1 Apr 2024 18:11:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE2F34E1CE; Mon, 1 Apr 2024 18:10:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="rHMkJeSL" 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 40F134E1C4 for ; Mon, 1 Apr 2024 18:10:50 +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=1711995051; cv=none; b=HVTpQl9MPi0XHE2t9huYWKpufka1MeetOms/vKNUxGynpXyIPlI01DRMu9YvIZFW1xGffK0czKpcggO2spwj2yIr7ORyMJVTmXuP2M5CwcCLYEoeQVdS78oyfFt0JzB3KwqPyIzNc/cNbEq6g29fwBdpV55CrmPCFUT78a8jXr0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711995051; c=relaxed/simple; bh=z2HNaLW7w9QpZHWGF0jaA9MyMN6bCQ1kVStq5aF7W4M=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=X4mskrPHb5tMm5B2MbtsYXKT61gO68dzSipxY/4YMJZVwDLrgk937YnWRP/Az5U690TYt//PS1MmHEKo0kSd9gUulG6KyZobNuUvdMCwfBtqmTQ+cPe0IXT+3rfPkImSHhuSqyzgXp0NZ3mhkI2H00/OBlXbL08NGXVx76SFfe8= 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=rHMkJeSL; 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 Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6ceade361so7893107276.0 for ; Mon, 01 Apr 2024 11:10:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711995049; x=1712599849; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=sw2vY2M4CeKEyqj973cP6PJDE6m3drwFn3wvpEExDXs=; b=rHMkJeSLIwbDSDjhAaUaHbGeo9ua3hm9of4MyQaLy0zZMvNGtvYEnEdula2es1QbPW pY4QSMRWt+1t/WE9SNVbgzlEVlFj8GGjMIaO0tar77U8CEsd/4yT+PIfWJYbsXsvAvbV uCCvztO/lH1qProrX3SxFuT+O/4ldTN1OWLZdVfhqgcxMvNAnVBKnxhFPFv6H2jgvOJ0 8FJLuRFXQwdPA7pnbfjNsj+s//nuXBraiIeZpaypwBXn5cbliWpVG+myT90fLVRhK9Ag SMgGj7StrbdgJVpr6fkS2+lUtNxDv0JNJq5Qmi0r1Mjk/99ISZ+LSOWL44a+iDi/OlYR 6Kzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711995049; x=1712599849; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sw2vY2M4CeKEyqj973cP6PJDE6m3drwFn3wvpEExDXs=; b=bWSOQ8yJDAskhyk/rx1pXXDxFV3wFvQUOw6ix8EDBSqGO3Obi82MjdLWToh2dPS+JS dwUzE6rOjYPcxIwesZgnVtXlGhYqOvjQliPVdb/e/HTl/LNMkHnRXE5iOuvAddy02MkE rnBIwgQtexCr42QNaMDzJVdCkp+EDoh0jmEz7hJqgznqRZ7ha4Ndjg52yQZ/6V0qfwa6 pxaaAuiQ78tfqrOB/6KF1ODtIwpKjfqe/b6p48ZvLcr43+StTp7A7zEIhPGSC3HRsbjJ Ya3Z36m+qzo5T5pNLHRPm22+ANsirk8ODwgbaSV3DLEO9zs41rjMVkvj8vKIprDEm2rF uQ/A== X-Forwarded-Encrypted: i=1; AJvYcCXpMK17pf+NactRJqmE/ZZfLDyyrVSfnlY0jXtUIgJ1cp8ipHaNlvbRPsQ8/aeU10CmPsmfI0Z/c0nX6jFta7vO249T1TgFPCm89+Fl X-Gm-Message-State: AOJu0YyjenoMjItfB11OFXdQ1RiKqZDN3WXDXgekMbBx6JmeiPk/iadA QkPGURBahPJ5wkvtsAeVA0H2nYtqfTRwWdcAZUkXV1m2sBC1CMDQzu75nq/nQnR3fooYjrJBknJ 7bnWB/yON7ghp4SHdc61IBw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:2313:b0:ddd:7581:1237 with SMTP id do19-20020a056902231300b00ddd75811237mr3279498ybb.3.1711995049368; Mon, 01 Apr 2024 11:10:49 -0700 (PDT) Date: Mon, 01 Apr 2024 18:10:48 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAKf4CmYC/43NQQ6CMBCF4auQWTumLQWNK+5hWGCZQhOkpFMJh PTuVk7g8nuL/x3AFBwxPIoDAq2OnZ8z1KUAM3bzQOj6bFBCaVEqiRzDbJYdLeNoeZk+jFsXY0C Dml6W6pvVpu4hB5ZA1m1n/Nlmj46jD/v5tcrf+ld2lShRmLq8V50UqpLN4P0w0dX4N7QppS9rB YvIxQAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1711995048; l=3756; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=z2HNaLW7w9QpZHWGF0jaA9MyMN6bCQ1kVStq5aF7W4M=; b=kU6IxVx3/F03byPgQEL8DLNLMSnWNCYWY3e9y+7keVoKq+5hvMV5hXsypcquKgXyj0ecnWlJ8 lvja9jJqSVCAxSvYraO/3hVEpDYa2ynuZ0XmjXrTG7v0QdUj3JsrDWG X-Mailer: b4 0.12.3 Message-ID: <20240401-strncpy-fs-hfsplus-xattr-c-v2-1-6e089999355e@google.com> Subject: [PATCH v2] hfsplus: refactor copy_name to not use strncpy From: Justin Stitt To: Kees Cook Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt Content-Type: text/plain; charset="utf-8" strncpy() is deprecated with NUL-terminated destination strings [1]. The copy_name() method does a lot of manual buffer manipulation to eventually arrive with its desired string. If we don't know the namespace this attr has or belongs to we want to prepend "osx." to our final string. Following this, we're copying xattr_name and doing a bizarre manual NUL-byte assignment with a memset where n=1. Really, we can use some more obvious string APIs to acomplish this, improving readability and security. Following the same control flow as before: if we don't know the namespace let's use scnprintf() to form our prefix + xattr_name pairing (while NUL-terminating too!). Otherwise, use strscpy() to return the number of bytes copied into our buffer. Additionally, for non-empty strings, include the NUL-byte in the length -- matching the behavior of the previous implementation. Note that strscpy() _can_ return -E2BIG but this is already handled by all callsites: In both hfsplus_listxattr_finder_info() and hfsplus_listxattr(), ret is already type ssize_t so we can change the return type of copy_name() to match (understanding that scnprintf()'s return type is different yet fully representable by ssize_t). Furthermore, listxattr() in fs/xattr.c is well-equipped to handle a potential -E2BIG return result from vfs_listxattr(): | ssize_t error; .. | error = vfs_listxattr(d, klist, size); | if (error > 0) { | if (size && copy_to_user(list, klist, error)) | error = -EFAULT; | } else if (error == -ERANGE && size >= XATTR_LIST_MAX) { | /* The file system tried to returned a list bigger | than XATTR_LIST_MAX bytes. Not possible. */ | error = -E2BIG; | } .. the error can potentially already be -E2BIG, skipping this else-if and ending up at the same state as other errors. 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 Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt --- Changes in v2: - include NUL-byte in length (thanks Kees) - reword commit message slightly - Link to v1: https://lore.kernel.org/r/20240321-strncpy-fs-hfsplus-xattr-c-v1-1-0c6385a10251@google.com --- --- fs/hfsplus/xattr.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/fs/hfsplus/xattr.c b/fs/hfsplus/xattr.c index 9c9ff6b8c6f7..5a400259ae74 100644 --- a/fs/hfsplus/xattr.c +++ b/fs/hfsplus/xattr.c @@ -400,21 +400,19 @@ static int name_len(const char *xattr_name, int xattr_name_len) return len; } -static int copy_name(char *buffer, const char *xattr_name, int name_len) +static ssize_t copy_name(char *buffer, const char *xattr_name, int name_len) { - int len = name_len; - int offset = 0; + ssize_t len; - if (!is_known_namespace(xattr_name)) { - memcpy(buffer, XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN); - offset += XATTR_MAC_OSX_PREFIX_LEN; - len += XATTR_MAC_OSX_PREFIX_LEN; - } - - strncpy(buffer + offset, xattr_name, name_len); - memset(buffer + offset + name_len, 0, 1); - len += 1; + if (!is_known_namespace(xattr_name)) + len = scnprintf(buffer, name_len + XATTR_MAC_OSX_PREFIX_LEN, + "%s%s", XATTR_MAC_OSX_PREFIX, xattr_name); + else + len = strscpy(buffer, xattr_name, name_len + 1); + /* include NUL-byte in length for non-empty name */ + if (len >= 0) + len++; return len; } --- base-commit: 241590e5a1d1b6219c8d3045c167f2fbcc076cbb change-id: 20240321-strncpy-fs-hfsplus-xattr-c-4ebfe67f4c6d Best regards, -- Justin Stitt