From patchwork Tue Mar 19 21:19: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: 13597071 Received: from mail-io1-f74.google.com (mail-io1-f74.google.com [209.85.166.74]) (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 45C964F1F9 for ; Tue, 19 Mar 2024 21:20:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710883202; cv=none; b=hsVY2hvWKwzZL9SmmACPwaPZeXnIQWko/nymG5nH6bU7wy27ze4UFvknlDFPE7ixpj+QsqFS+JlwhmGoFvUnZoTaA6LO2wJoitCbc0RHshCZETWwzJ6y4tA0+vByU7E4vVjMIK54O4yU3Vhz/ttwtUFtL6JeGCXie2Uvr9vjDMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710883202; c=relaxed/simple; bh=Mdg4v+gVR1XXoJ7LDiKyb/js1DlYqwa9cvruL0+pdyw=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=ZhkIlF5+vrPLoO7UXtdY+UkEx2o70b4+iyMrphumSJ0h7QUeSzZoP3YBw0fOTjFL7JgZQ/kahJdxzgbnbXTioPzNZg4Ip+T673JEB46ybU+GtyaitCiImax2zrpgJ69ypZwrNO63Eba4WTUnqXGp3Sry7C6bYT7rdvo7JqCmw7Y= 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=RmfLCLnL; arc=none smtp.client-ip=209.85.166.74 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="RmfLCLnL" Received: by mail-io1-f74.google.com with SMTP id ca18e2360f4ac-7cc044e7456so282908539f.1 for ; Tue, 19 Mar 2024 14:20:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710883200; x=1711488000; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=+P2giq0KAVuF343w/p3qlKL7+PcOe1+28vHCwGD1ebk=; b=RmfLCLnLmsR9mhY3CO9C4W3I/PUHsHFJIbovRwyI0rs07KY/tcsHC4cZ4XccrlHgEs bBxJ0/3MZRMDkwD6WLAOpWnEDY4FSoATZf7xbBASfMyIeuVRhgdJ/8XgsApTVqBZRB4F CFALffKel050sCsHst0biguvm7miVHy326nF1baL0hXUHXy1Noe5XZmOBrVVOSkMnozR bHAazkPyloya5GSO3CLap49iqRCi7SlXXd5VwEanCwkc43MjbxK/zKoF3EIBVY5M8px+ JAJrXReqNYOshyNx0+6a34AN2kcDaMawlOUPHRi3jLFys8nkAfpsw90wQsgcRWzGf5bV CCYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710883200; x=1711488000; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+P2giq0KAVuF343w/p3qlKL7+PcOe1+28vHCwGD1ebk=; b=mscXRkDjPJ+Ztu/oqEQyLMY4XujknG9GCRm9Jp4SJijoJ/zPqKIQSFkmUZL24xlSU1 ovTi9Vp7ofqvPThljsbdOEenrkC455MWVVV5SFlXJxxYo9sj5u2N/OYr+hB6yNqPWpCl SNcd9Xd4KTuaMvo44Wo9atmolCS6AhnWzkv70kPHsHcBhUJqSHVBjvIHMiYMK406RZUg BpOPE6XxngNSW+wi2/ugnBdG7LJF8KBUiwmEC0tIx+9HSRiMXiGtnzKXlFupzRUQx2T8 aA9wMaj3/d5ZhCoBWTlUgbDlV1LLqnWx0plFYKwCAu3wKUiOgAZSV0NM3PGW9fqhX+8K 2G0A== X-Forwarded-Encrypted: i=1; AJvYcCVtG9R/peahplx9m9VSwQXZ5+jV9xMxlpTusxa3MrjPoGjPkFsqYCdk404sOW0SZ8afEB5pEpnONMmUDZ931HlZNWSJRc2PjggZ/1ITr/UP X-Gm-Message-State: AOJu0Yww0UxI4qQYFttGmhFjzAlkdUmLz7I+E3LNgVHzc+Ox5LCJ9rF2 TWHJbvRq35zeQqbGqEgqQSw50M1VuCxnX3RBx7gKtGNQmctlt3bxDyLnBmpHmm6vdrCyq1a/5TO q6G4k/nWDc+f7WpAUjPTNUQ== X-Google-Smtp-Source: AGHT+IEjYa8XAuzaZ3QDjFc+CmwpKb+B2fiaTFJKL46VazLIXLWNGSzFGXrx+jPM3IbTpLMVy06PiFtlUIAyiHMl5Q== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6602:2c94:b0:7cc:342:72f9 with SMTP id i20-20020a0566022c9400b007cc034272f9mr297570iow.2.1710883200494; Tue, 19 Mar 2024 14:20:00 -0700 (PDT) Date: Tue, 19 Mar 2024 21:19:59 +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=H4sIAH8B+mUC/5XNTQ6CMBCG4auQrh1DW5DqynsYF/2ZQhOh2JJGQ ri7hZXudPlOJs+3kIjBYSSXYiEBk4vODzn4oSC6k0OL4ExuwkpWlZxWEKcw6HEGE1zCECF6DU/ te9C9AaNAAxOV5VJJqZQg2RkDWvfaN2733J2Lkw/zPpnodv1HTxQoNKWtTY1N2XB2bb1vH3jMX 2TjE/skxS8ky6SwJ1SWKiPO9Itc1/UNgE518iYBAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1710883199; l=2271; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=Mdg4v+gVR1XXoJ7LDiKyb/js1DlYqwa9cvruL0+pdyw=; b=Fu5AbgciDR7kyPaRIoo73doyAxlV6sVzOVwcGzj5a0Hjq/yMcTBMrykb+d+ZRRZdAIphvOzje LT65OpzS84VCMIjD2l5Sw4sVwE+biQfYsuW0wkHVZ0og9OiNliktfT7 X-Mailer: b4 0.12.3 Message-ID: <20240319-strncpy-drivers-soc-qcom-cmd-db-c-v3-1-aeb5c5180c32@google.com> Subject: [PATCH v3] soc: qcom: cmd-db: replace deprecated strncpy with strtomem From: Justin Stitt To: Bjorn Andersson , Konrad Dybcio Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Kees Cook , Justin Stitt strncpy() is an ambiguous and potentially dangerous interface [1]. We should prefer more robust and less ambiguous alternatives. @query is marked as __nonstring and doesn't need to be NUL-terminated. Since we are doing a string to memory copy, we can use the aptly named "strtomem" -- specifically, the "pad" variant to also ensure NUL-padding throughout the destination buffer. 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 Reviewed-by: Kees Cook Signed-off-by: Justin Stitt --- Changes in v3: - Prefer strtomem_pad (thanks Bjorn) - Reword commit message (thanks Bjorn) - Carry over Kees' RB as this new version is functionally the same and matches the same spirit of v2 - Link to v2: https://lore.kernel.org/r/20240318-strncpy-drivers-soc-qcom-cmd-db-c-v2-1-8f6ebf1bd891@google.com Changes in v2: - use strtomem instead of memcpy (thanks Kees) - Link to v1: https://lore.kernel.org/r/20240314-strncpy-drivers-soc-qcom-cmd-db-c-v1-1-70f5d5e70732@google.com --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/soc/qcom/cmd-db.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) --- base-commit: fe46a7dd189e25604716c03576d05ac8a5209743 change-id: 20240314-strncpy-drivers-soc-qcom-cmd-db-c-284f3abaabb8 Best regards, -- Justin Stitt diff --git a/drivers/soc/qcom/cmd-db.c b/drivers/soc/qcom/cmd-db.c index a5fd68411bed..d51ae99d9c02 100644 --- a/drivers/soc/qcom/cmd-db.c +++ b/drivers/soc/qcom/cmd-db.c @@ -147,12 +147,7 @@ static int cmd_db_get_header(const char *id, const struct entry_header **eh, if (ret) return ret; - /* - * Pad out query string to same length as in DB. NOTE: the output - * query string is not necessarily '\0' terminated if it bumps up - * against the max size. That's OK and expected. - */ - strncpy(query, id, sizeof(query)); + strtomem_pad(query, id, 0); for (i = 0; i < MAX_SLV_ID; i++) { rsc_hdr = &cmd_db_header->header[i];