Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1131917rdb; Tue, 19 Sep 2023 23:51:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE1ieBQbAb+W4YkuoeU2jb0Isa2D60WjbZ+Xjou+ciEDuUzLidZhfPy+ZVsO/B8ANbZb4VM X-Received: by 2002:aca:6741:0:b0:3a7:3792:5b8a with SMTP id b1-20020aca6741000000b003a737925b8amr1497366oiy.18.1695192660305; Tue, 19 Sep 2023 23:51:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695192660; cv=none; d=google.com; s=arc-20160816; b=qE6ry+X+pl+DR/MOQQ3fNEhLDzlKv2g/y+AmLWtqkh4pGJFcIhs1nDSArk4MVlEUAQ 9x0FjKRSY9x7SBpoUIZGmndRAXf0j7XvOeNKF5gUgEl89LDdEZReDkic5zsq1CWcI0DJ wnxGr3t7xJtw/dyTdSDefvgsrL+ea6T/yNLeqt9jFxgxZ9b6vZayIN7vfd2vCR1Mv061 5xtwx7fTXiiDN5yil1MmaJ4v7s3PgPUvU66TTxvUINZQiUh7LaTG8O5gNK4B6BOkTX4X rFmHravEPUwbdVBgLTwz9d5pqMoWTg/KtLMP03CiXv3RvBK3LroqF9OHdwyYKArn1pth DBlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=5Yw61MTv6sQm8GtqaAIRGAI4x3jcX/Zyv9SKpJ6pf7k=; fh=bzn2F3HY+Pzx6Tu+v5yttU3vqwU7uYsi+lD4qyt+PLY=; b=fOLq95DszLD096Qyr6hxS3YJdpzwSeaMxtxWx5c9s+VOx+iKcGRfuQMA5+5XYe0HI5 v0KX3ndv7Pf1ehYOSUDo8CE3b8BCLgcMqa2rNk2huBZkmbCvEtC4m/oHX1vDlIx/A6AX 0ypwfzQn9sBLvJ+mbADGAbeKFShAbGIV8GMNpbK1IFixr2JdFZwp7ZN1nGfw2U0duUhC FAp92i5mLWzDWhGlrIUecjQEA140GBTujcKtGLOiaf7AeHMNwF1lLdQH/V1Ymn3lYRTt arhIUCnR/w4+TMz9F1RCIXvi3FLuAz2/2r1BvrZ587/+swjJZLF2SdCAJmHhN9jmplLO +lGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="4QCx/IPB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id v70-20020a638949000000b00578b4992008si2635331pgd.133.2023.09.19.23.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 23:51:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="4QCx/IPB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 0E52580AE83F; Tue, 19 Sep 2023 20:21:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232376AbjITDV0 (ORCPT + 99 others); Tue, 19 Sep 2023 23:21:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231630AbjITDVZ (ORCPT ); Tue, 19 Sep 2023 23:21:25 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 207C9B9 for ; Tue, 19 Sep 2023 20:21:18 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-9ae22bf33a0so99758766b.0 for ; Tue, 19 Sep 2023 20:21:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695180076; x=1695784876; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5Yw61MTv6sQm8GtqaAIRGAI4x3jcX/Zyv9SKpJ6pf7k=; b=4QCx/IPBWbtWvCQmBWJrBoEpFj9ZrEBZ+ZbK62lpiL4hXTT5AIiPDnBdViwIdZKsth LvuFCHkHMSMJnM5TIOFcaEMawcMUqGd2b/sDnNngcyxRgEtMm0fOwIctkwRHBhQMiTHz FpnQVc/2qSoRRaW+LVahObYnjoQnvpFYb2iSOm4I1fz+bHbKJCnvTnUSOXD2tVqmI014 VjW8dwirjP7/IwP+bc6Z86X5kidrU7E/A009K3adgSFQzKfncpA9ejR5QI2fooBgqc7Z K9OJI3R9k7Rv+r40b5YGnKHCZut89qmjyjahzboEhHJ3Qc5Hc0JovWejnuJBi4IcOKta O7+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695180076; x=1695784876; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5Yw61MTv6sQm8GtqaAIRGAI4x3jcX/Zyv9SKpJ6pf7k=; b=mdRN9U5G+wkFte0iswklA7jHLlhq16lxIHVElcKUAk6G+Gjq65bDgSx0jZqg+EJ7xc GuMLSwoyc30x6mQpsr8yCpDU7VZL3TJok0o1eOnUDWwnGE5nloIs/sQZzJs76FsvhxZR f4jo86JQZMJP8bUYxNF3ILQsVGQe/WhZrvNCe5IMRvr3j9PRJK+ILKW8pwEtNeBOq+Kw rB0q0GK8OeFsUtmDw3K5jcviymyL94TfoPUbQf/16X3a5QMWUBSEAv0ZaGoVll8piBUE vhGCVj0Jou4nSQlT2ECvzEG+HIui/Gw6gLamrizbnXktXvNBDMHu9iv2pMkuUabMTwBX 2npA== X-Gm-Message-State: AOJu0YxW1uvouIL7sllP7NiXUErA7Jbiq2tNXjTuktjctLcyJLwlyti0 Kn2R69ucbq+zJ9Du4psG4IU8XsKRUglZSYw26UrEdA== X-Received: by 2002:a17:906:329b:b0:9ad:b37f:9615 with SMTP id 27-20020a170906329b00b009adb37f9615mr2030017ejw.29.1695180076517; Tue, 19 Sep 2023 20:21:16 -0700 (PDT) MIME-Version: 1.0 References: <20230919192156.121503-1-azeems@google.com> In-Reply-To: <20230919192156.121503-1-azeems@google.com> From: Justin Stitt Date: Tue, 19 Sep 2023 20:21:05 -0700 Message-ID: Subject: Re: [PATCH v2] vt: Replace strlcpy with strscpy To: Azeem Shaikh Cc: Greg Kroah-Hartman , Jiri Slaby , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, Ard Biesheuvel , Andrew Morton , Tony Luck , Kefeng Wang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 19 Sep 2023 20:21:28 -0700 (PDT) On Tue, Sep 19, 2023 at 12:22=E2=80=AFPM Azeem Shaikh w= rote: > > strlcpy() reads the entire source buffer first and returns the size of > the source string, not the destination string, which can be accidentally > misused [1]. > > The copy_to_user() call uses @len returned from strlcpy() directly > without checking its value. This could potentially lead to read > overflow. There is no existing bug since @len is always guaranteed to be > greater than hardcoded strings in @func_table[kb_func]. But as written > it is very fragile and specifically uses a strlcpy() result without sanit= y > checking and using it to copy to userspace. > > In an effort to remove strlcpy() completely [2], replace > strlcpy() here with strscpy(). > > [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcp= y > [2] https://github.com/KSPP/linux/issues/89 > > Signed-off-by: Azeem Shaikh Reviewed-by: Justin Stitt > --- > v2: > * Return -ENOSPC instead of -EFAULT in case of truncation. > * Update commit log to clarify that there is no exploitable bug but inst= ead the code uses a fragile anti-pattern. > > v1: > * https://lore.kernel.org/all/20230830160410.3820390-1-azeemshaikh38@gma= il.com/ > > drivers/tty/vt/keyboard.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c > index 1fe6107b539b..12a192e1196b 100644 > --- a/drivers/tty/vt/keyboard.c > +++ b/drivers/tty/vt/keyboard.c > @@ -2079,12 +2079,15 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __= user *user_kdgkb, int perm) > return -ENOMEM; > > spin_lock_irqsave(&func_buf_lock, flags); > - len =3D strlcpy(kbs, func_table[kb_func] ? : "", len); > + len =3D strscpy(kbs, func_table[kb_func] ? : "", len); > spin_unlock_irqrestore(&func_buf_lock, flags); > > + if (len < 0) { > + ret =3D -ENOSPC; > + break; > + } > ret =3D copy_to_user(user_kdgkb->kb_string, kbs, len + 1)= ? > -EFAULT : 0; > - > break; > } > case KDSKBSENT: > -- > 2.42.0.459.ge4e396fd5e-goog > > whitespace nitpicks: A newline after the newly added if-statement's ending curly brace as well as reinstating the removed newline in your diff might make the code look "better". Thanks Justin