Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3760791pxk; Tue, 29 Sep 2020 05:44:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJys35wRn59VuFfxVNRnRFKjIVVwqFzCqNGCRlez+kGZgZ155qTcdYH7ODNk5eRjx/GNX2hd X-Received: by 2002:a17:906:b24e:: with SMTP id ce14mr3588615ejb.494.1601383495051; Tue, 29 Sep 2020 05:44:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601383495; cv=none; d=google.com; s=arc-20160816; b=wVAnm2edUBL3SQ/ZGZ+q8IzHgUywlcsdehFCyazapqklAqdJEeclj2IFQLrKWnpdxX Vu65Q8lUkx+L0ce1zB2guppVm3+p9vjOhLPvDwCZA4+TxENs78+cx2PnpDIakdFEWMbJ nebS8jmC5Wnej7n5KYF/WKACKhFrvycq0OWkPSLDvxOu9XDPgEN1Aj05+vnIJC5IStiE yr4Ich++66pFgv4huKWNXpJ4+h0KkNXilqf8SqDB2GRXu0Z+dLjsGfJfyXxyxbHTvfZE LLfR82ozrMObac/XTra01HRkLtNiBNJnlbWUiXi2klUiBoGXpdjIjbhbaZleLptgdFrt khsA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tfpm2ZgwhTU6Ld38smgpwrPYqDM97nuFDg1PTqHBZrw=; b=tJZh76uTmX1vvt8tm1DWsfYZAiwIrkuh5VmuHTejvlcxu9uDhydQ2wyYzuG7S7mRRY HfFBghg2yHObOzdoPJKKFdO+bejsa8qTMF/fEHlGkS+kR1vR6Ah7Aryd17Z4ThVOPvQs Qh9rOEX+iSRPsgUIM9PcvQQupKYaTEY7rLfxVMcm8YM78mR30GMW/tyZK55K5XI8ZN+c F2xmEMhx7DPcGQWsCpW6vHIXF/EgwtU6Iql+Gm1GMUU/4A7vM3Lwttgrn9ObwKCNyQFR tfrrJazh9IxXW2uG41AUw67gGYQZR0B32qPhLh7dJ6ZVkIH2/n/epcu2rE8A9VMfgyAS N81Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Zqkmojtc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x23si2625780ejc.63.2020.09.29.05.44.31; Tue, 29 Sep 2020 05:44:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Zqkmojtc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733161AbgI2Mng (ORCPT + 99 others); Tue, 29 Sep 2020 08:43:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:50308 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729093AbgI2LJs (ORCPT ); Tue, 29 Sep 2020 07:09:48 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 10C61221EC; Tue, 29 Sep 2020 11:09:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601377777; bh=ffVmtk+MMzKg1ZNQEVX/lHavatGWRWr6XMVURgNQYTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZqkmojtcrqzS39t8Stw9CungmVOnaOX8K7aEy+1Ggo2RMcsIdNP4+V+/tKIC4K2qy 6dZOMSv5o+jMocGSo01gZoJA/KEOFhRFlmY9cnwdYp4ywDG5PW96QL9vMHtT99Dm9U swGVAqgOUf2fQgfPOXpQ4t4cGGX+R6ej1c0L7ySE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christophe JAILLET , Chuck Lever , Sasha Levin Subject: [PATCH 4.9 069/121] SUNRPC: Fix a potential buffer overflow in svc_print_xprts() Date: Tue, 29 Sep 2020 13:00:13 +0200 Message-Id: <20200929105933.588887336@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200929105930.172747117@linuxfoundation.org> References: <20200929105930.172747117@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christophe JAILLET [ Upstream commit b25b60d7bfb02a74bc3c2d998e09aab159df8059 ] 'maxlen' is the total size of the destination buffer. There is only one caller and this value is 256. When we compute the size already used and what we would like to add in the buffer, the trailling NULL character is not taken into account. However, this trailling character will be added by the 'strcat' once we have checked that we have enough place. So, there is a off-by-one issue and 1 byte of the stack could be erroneously overwridden. Take into account the trailling NULL, when checking if there is enough place in the destination buffer. While at it, also replace a 'sprintf' by a safer 'snprintf', check for output truncation and avoid a superfluous 'strlen'. Fixes: dc9a16e49dbba ("svc: Add /proc/sys/sunrpc/transport files") Signed-off-by: Christophe JAILLET [ cel: very minor fix to documenting comment Signed-off-by: Chuck Lever Signed-off-by: Sasha Levin --- net/sunrpc/svc_xprt.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c index 42ce3ed216376..56e4ac8e2e994 100644 --- a/net/sunrpc/svc_xprt.c +++ b/net/sunrpc/svc_xprt.c @@ -103,8 +103,17 @@ void svc_unreg_xprt_class(struct svc_xprt_class *xcl) } EXPORT_SYMBOL_GPL(svc_unreg_xprt_class); -/* - * Format the transport list for printing +/** + * svc_print_xprts - Format the transport list for printing + * @buf: target buffer for formatted address + * @maxlen: length of target buffer + * + * Fills in @buf with a string containing a list of transport names, each name + * terminated with '\n'. If the buffer is too small, some entries may be + * missing, but it is guaranteed that all lines in the output buffer are + * complete. + * + * Returns positive length of the filled-in string. */ int svc_print_xprts(char *buf, int maxlen) { @@ -117,9 +126,9 @@ int svc_print_xprts(char *buf, int maxlen) list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { int slen; - sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload); - slen = strlen(tmpstr); - if (len + slen > maxlen) + slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n", + xcl->xcl_name, xcl->xcl_max_payload); + if (slen >= sizeof(tmpstr) || len + slen >= maxlen) break; len += slen; strcat(buf, tmpstr); -- 2.25.1