From patchwork Wed Jul 26 21:59:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 126567 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp728068vqo; Wed, 26 Jul 2023 16:34:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlEIK6w1r0ohfhKkoQ8OiZuoeq9CWEhMGJRv3w9VmYG1/V1jTcMxXjr982ZxsKqA+uiKgK1z X-Received: by 2002:a17:90a:de82:b0:262:e589:678f with SMTP id n2-20020a17090ade8200b00262e589678fmr1045908pjv.10.1690414444950; Wed, 26 Jul 2023 16:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690414444; cv=none; d=google.com; s=arc-20160816; b=n0p8hCiaXNfRsNXjCEFw8A3n9vzis+GyBx7e5B3fTHuQ8isZqmRszTO7IAen7ANmLG PS4VqjujtX59/LdFuPxnvqYzXmGj3IIKrdB2V8GXRJD/hgKxyVfAminp7CerqIOs6yfK 0z3SFOUFb04ezFpe3//X6YJaTdoZjRtMP8qpvVLzTB+TJoXG4JfFq8x3hpa36qp4Mcvh ClxETQv2mxLbqQsPovGklPADjSlzZp6mOnQq0zG0O1zaYa4+pzXcoHC9/qpWNu5iTtfb ITrbhe5l/9cMG6kZnCnfYW5TXWNC7LYCPBiMoAShBDmJgHulgEL0KQtLXQl7Ls4hbR4u /Szw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=CmO8f5N+/Wc0ClX0Q4hB54MuVMgQHt4gjqV/2VzUoJM=; fh=lNfO8DmXcYrnn6ICc4nhMGdw3GhbQoPArb2Nc++HKyg=; b=AvvIkxoSonEnSvoIXKQjTNCPHZ3jN0v0f8DjdTBpEYl+FRJluBZO8W5gYTyMAgOFkq cgoOnEcU/G+Mix6ph6QJYpQJ80ZBMg1sO9ab6EYrxtC2d6PwYXrXU2RrLmzlwg7jVnB+ hcnetDMJFmpMwOuQp4zZXmakH4Lsew4nBnx5y6uA8ARqC8+boiPkIiA6It+IQPXlWfKl MG/L7zv2tY8mgmsSs8S+rZa9DnTA+ur8zaibDIjTUMhhr4CeGXCUnZx3W+vEnhXkmO00 N6/0A2cHCA4Teb0YtjkJch31RiIZL7r/hN39zmmyVcISK19q8dKWHFUCVi7/vrcxwbRt gzYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IRheo1Vc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mp22-20020a17090b191600b00262e6b05c92si1723408pjb.150.2023.07.26.16.33.51; Wed, 26 Jul 2023 16:34:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IRheo1Vc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230110AbjGZWAC (ORCPT + 99 others); Wed, 26 Jul 2023 18:00:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229574AbjGZWAB (ORCPT ); Wed, 26 Jul 2023 18:00:01 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7477E1BF2 for ; Wed, 26 Jul 2023 15:00:00 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6726d5d92afso997497b3a.1 for ; Wed, 26 Jul 2023 15:00:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690408800; x=1691013600; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=CmO8f5N+/Wc0ClX0Q4hB54MuVMgQHt4gjqV/2VzUoJM=; b=IRheo1VcS17JRchWvrxtHhXbdTmCAHaaaMFvWr7HxzXgp/f+zpdEeRiicU5YVpXF5O fqxPZ7YCFduxv1q8b0/HLyrWvvb3Su1hY17PYxYOiCuHUwjqKHH0TSYWpgsZsnCCuKJ/ 93h83WY9D0JzgNxc/8DbJePLzoXjFCZYFcb3A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690408800; x=1691013600; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CmO8f5N+/Wc0ClX0Q4hB54MuVMgQHt4gjqV/2VzUoJM=; b=X8fd+KaZlWPu9XC3ZPLfi7Ij8i0KRGORTGUyoQh8rjbTqMAfzH5SqYaaQpJHvVpzQL I4kk3YIn90LdPVEPqsS+pNbqiONw1fljV32zjJXtjg5jRrdNkRvboqXfi+T6a8pmdNhi MvLNVLjZ8DsG20fsZCnO8m1JLnH2qTd8vI1wzNAiGGx0vY000XqeqDqk2JqfQRdLC8z9 LKChtMXzCBWInh6aTHs7B4HAIjZurQ7rkxOgmj7yXDwZL+31ii+IQrLgz1YNLDQZlVxQ QE6adsgjJ/0JkBizMMRy3tRKMauxz4CTzkbaCQp4zLqMN7DbOD1yGtrif0c5gtshAd5C yt3Q== X-Gm-Message-State: ABy/qLb82i1dloggzNRxkBuPzbr1JmFhODEa7Px5388lJtILo4BO80+I 1WUlyCv+ou57e4MG5ygjeM7Mgi3hMQ1Wv9sNZbY= X-Received: by 2002:a05:6a20:3d8b:b0:128:ffb7:dcfe with SMTP id s11-20020a056a203d8b00b00128ffb7dcfemr859241pzi.1.1690408799887; Wed, 26 Jul 2023 14:59:59 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id j25-20020aa78dd9000000b0067777e960d9sm70369pfr.155.2023.07.26.14.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 14:59:59 -0700 (PDT) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , Andrew Morton , Al Viro , Muchun Song , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] seq_file: seq_show_option_n() is used for precise sizes Date: Wed, 26 Jul 2023 14:59:57 -0700 Message-Id: <20230726215957.never.619-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1628; i=keescook@chromium.org; h=from:subject:message-id; bh=5SG2UTPOhvYfjLu1GmFk1JjaJdwmNUKnlU3NSz2ezqs=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBkwZdde36GoULThoErTSucEyxJhlW0LYit40bsm ZxXjkmRXoGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZMGXXQAKCRCJcvTf3G3A JvX2D/46MlSRKOZrRkZWdErmLSLB6JVxuSpANsljamLtuYJybYwdTTB0FWf7OwGlmEi9NpC+o4k O9q/f3yPK1/wv+iFiNSrYzeUvnnD1FWQlVSX2ktFhPL61zUitEjUKbyATnEnEy7SVIg86FfiYvz tNkdTtU6QaXSU6B2kiir4wnNxt0k7FpE0orAqIy8iTe5wJTERBnDPkvdg3OPqIa7UtlFzrw9MGF gUEwUOzSU7T0b3/Bo49+rjlHfEjmqMTc1GfLX+M6KB7UK57ucIWzAMcXuVMgpagZo3/OKej1EeH 35hPiXkmesiRd/MEa1VwJYPzs1xKHfkhnsd/5vTINLwBG2fcMSDTx/I2RWcip//oQSY3T0CaqkI pmh7K/u8cEKZydD66d7A7cZ7amu5t4x7A84Iol+d/wRwYRluCyjW+mkpgo/zmmlUGkMzLBvJS8P lJOPcdKXb6vj1LJzFIoBYFsX9vXYXpCxCbX3kgs+gcDC4HdM4yqhy6S/NsvmpZUM8c8/9e8Xngf 23sotyX5AKaubcL2cuf953pSQ1AyAot9i7JC66zaW9quZCcnICCbzjSajaA0Ai/sdG9Cl04lXHR uDIdgkmvTfHiaOkHcWD3zkbHFXQ50baLiOEVNveTNVKd8XF8JZykTEmjxitI0kP2Uz/zt7Wc+bO 2ox8Mby q0xyXc1w== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772528016858489367 X-GMAIL-MSGID: 1772528016858489367 When seq_show_option_n() is used, it is for non-string memory that happens to be printable bytes. As such, we must use memcpy() to copy the bytes and then explicitly NUL-terminate the result. Cc: Andy Shevchenko Cc: Andrew Morton Cc: Al Viro Cc: Muchun Song Signed-off-by: Kees Cook Reviewed-by: Andy Shevchenko --- include/linux/seq_file.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index bd023dd38ae6..386ab580b839 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -249,18 +249,19 @@ static inline void seq_show_option(struct seq_file *m, const char *name, /** * seq_show_option_n - display mount options with appropriate escapes - * where @value must be a specific length. + * where @value must be a specific length (i.e. + * not NUL-terminated). * @m: the seq_file handle * @name: the mount option name * @value: the mount option name's value, cannot be NULL - * @length: the length of @value to display + * @length: the exact length of @value to display, must be constant expression * * This is a macro since this uses "length" to define the size of the * stack buffer. */ #define seq_show_option_n(m, name, value, length) { \ char val_buf[length + 1]; \ - strncpy(val_buf, value, length); \ + memcpy(val_buf, value, length); \ val_buf[length] = '\0'; \ seq_show_option(m, name, val_buf); \ }