From patchwork Mon Sep 18 05:30:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13388821 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A308CD13D1 for ; Mon, 18 Sep 2023 05:31:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239595AbjIRFaq (ORCPT ); Mon, 18 Sep 2023 01:30:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239690AbjIRFac (ORCPT ); Mon, 18 Sep 2023 01:30:32 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2DD0129 for ; Sun, 17 Sep 2023 22:30:24 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59e9a08f882so7480757b3.3 for ; Sun, 17 Sep 2023 22:30:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695015024; x=1695619824; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=OE+ciIbOkP2gbp6T+0Gt73rrnC9CJepjybcVkk4F57E=; b=SpN7mw8PtIDl103YYs8oSKP3kAl88Fc6exiO7BLsAsY/lhqk/GzBINKq3ANjhiMO/O P6WDoBy8Zl+cszPWDH+eT1x6nTTE/RipX/tpznLD96JRM8QGkIMwaZY4rdciVjbXcUlM 2pRyDnoajiz/+jRmtAFGGUVb+3ke/gB9CMj6VeXUzsP+KNWTjksGGXOCdSRyYPW4WaCd dYaOOQzTzUnqLC1J1zM0T6H0CqG5QlusY9ltFR8Gn/Coj8WT+1934GBJMaaYg154SklK c4T2Y7tPkd93qIGN+1QvPNqi+tjV7PUTVJZLON9HYnNTLc7Bt1b3Z4cPaU5Yui1G5A8T fB+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695015024; x=1695619824; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=OE+ciIbOkP2gbp6T+0Gt73rrnC9CJepjybcVkk4F57E=; b=iN6Ol4Hp9YUhTGoLkGJI7f0p1l6OgLJrmkL/O6/E33u91zAWPqpWW5A/3Cra8ot8zh rB+N0J+q7l4QxxXJw4cI/EYzupj4qwErYVOeA/f/5Y7pp9C2jXE/wUtcE9tDCeew9NiO TSk0jhdaSovG+RbleWiJt0TdoWSQ0fwwSob2OkzTcOib5Asausy8jXJF1y3OV0OgyC7g zOKlFOcM+JHhXtZudwQgknhzj/iMIz0Q9anumVSIQylmNT1si0RIkKUgAQk0yhpCounA VnFtMAhMROcD3X83UT3Ydl63QCOtDqb4GHCrl+bduBOoLJhxTd05BIyw7kqjaFrYgYeG oaZQ== X-Gm-Message-State: AOJu0YwuDIyIpk9bNbGcBAs+3mEfKUbM4ZEuSGQdzrjfTeM4QyzQ1n3O Uu3xtKfuwnpYsHRujs2vVMNNMvYODl9FclkawQ== X-Google-Smtp-Source: AGHT+IFJNgXq1W9sUxEvFZxM8KaPscNzVC69QUb0G5ux6YHJXL5EoxRGPEba+eN4+1WufJgCtzYHzwVgUY9hITAoJA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a81:b61c:0:b0:59b:edd3:fdab with SMTP id u28-20020a81b61c000000b0059bedd3fdabmr201776ywh.4.1695015023902; Sun, 17 Sep 2023 22:30:23 -0700 (PDT) Date: Mon, 18 Sep 2023 05:30:15 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAGbgB2UC/6WOQQ6CMBBFr2K6dkxbiIor72FYlDLACNKmUxsJ4 e5WruDyvcV/fxWMgZDF7bCKgImY3JxBHw/CDmbuEajNLLTUhbzqEjiG2foF2kAJA4OxFpmpoYn iAuzRjG8Po2ueaCODhVLKCi9tJ6viLPKsD9jRZ08+6swDcXRh2R8k9bN/xJICBYVR2CmtFVby3 jvXT3iy7iXqbdu+gL2JHfEAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1695015023; l=2339; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=ZRAevJITjeQ3gig5kuCzdgxw+0nnBqh8Vks2uPOz8VQ=; b=BYbtuoA82mn5rA+ynVQEmlpFtq1oD0knVDz6B12KBhox6dKe5/z1nyregyISK/hk/tpEvtR5/ eF7t0vp8tMSD54eybkINr+clScIJ2CZP32kGrPV8WDKn1C3F2hocgYS X-Mailer: b4 0.12.3 Message-ID: <20230918-strncpy-drivers-accessibility-speakup-kobjects-c-v2-1-d5b1976c5dbf@google.com> Subject: [PATCH v2] accessibility: speakup: refactor deprecated strncpy From: Justin Stitt To: William Hubbs , Chris Brannon , Kirk Reiser , Samuel Thibault Cc: speakup@linux-speakup.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt , Kees Cook Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org `strncpy` is deprecated for use on NUL-terminated destination strings [1]. Let's refactor this function to just use synth_write(). Link: 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 Suggested-by: Kees Cook Reviewed-by: Kees Cook Tested-by: Samuel Thibault --- Changes from v1 -> v2: - refactor synth_direct_store (thanks Kees) - rebase onto 3669558bdf354cd352be955ef2764cde6a9bf5ec - Link to v1: https://lore.kernel.org/all/20230824-strncpy-drivers-accessibility-speakup-kobjects-c-v1-1-3a1ef1221e90@google.com/ --- drivers/accessibility/speakup/kobjects.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) --- base-commit: 3669558bdf354cd352be955ef2764cde6a9bf5ec change-id: 20230824-strncpy-drivers-accessibility-speakup-kobjects-c-4009e7df0936 Best regards, -- Justin Stitt diff --git a/drivers/accessibility/speakup/kobjects.c b/drivers/accessibility/speakup/kobjects.c index a7522d409802..0dfdb6608e02 100644 --- a/drivers/accessibility/speakup/kobjects.c +++ b/drivers/accessibility/speakup/kobjects.c @@ -413,27 +413,24 @@ static ssize_t synth_direct_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { - u_char tmp[256]; - int len; - int bytes; - const char *ptr = buf; + char *unescaped; unsigned long flags; if (!synth) return -EPERM; - len = strlen(buf); + unescaped = kstrdup(buf, GFP_KERNEL); + if (!unescaped) + return -ENOMEM; + + string_unescape_any_inplace(unescaped); + spin_lock_irqsave(&speakup_info.spinlock, flags); - while (len > 0) { - bytes = min_t(size_t, len, 250); - strncpy(tmp, ptr, bytes); - tmp[bytes] = '\0'; - string_unescape_any_inplace(tmp); - synth_printf("%s", tmp); - ptr += bytes; - len -= bytes; - } + synth_write(unescaped, strlen(unescaped)); spin_unlock_irqrestore(&speakup_info.spinlock, flags); + + kfree(unescaped); + return count; }