Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp876638pxk; Thu, 17 Sep 2020 20:03:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxph9Y+qfks9M0VPItLu9HYbhLpHPmVkB19Jp9oicOtZD21fGuX0paZ2j4BBBDN0DSGTSma X-Received: by 2002:a17:906:4a81:: with SMTP id x1mr30536022eju.541.1600398223323; Thu, 17 Sep 2020 20:03:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398223; cv=none; d=google.com; s=arc-20160816; b=h9B6+C3wbRbEM6ZWWEasKn7Miy7LmKl5h0gCtpUZwnB4C28+exbrsSGNaG1sq83T47 HdNEpdirf4v4/FDw1RuU5XZ+/AdQ27bEGwvmClKjRFIxVN7+emdpB9uUEk7C3lKapQxl Q/BrqxpOl/6v5j6lx/LfvmAgsemnKYG25foT6B8daNKVtz7Y1+PfUtZPPBvn+BqecCfb SeuJX0CuVjSrGyURfiUp9/k5U51JyMRu5OkaP0IWHcCqEWlLcArgHpTnofOHaWs6qk6E Z5U2R0T9L+6cU8dZse0tJKdY1BhFNIM263KIbvCOfrXFxk3FC9dVLVFwq0gVSe2pEhFE Zi5Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=11yq9czAF2kSHAXgqwSVE3zKQmJokzxLIY2wgg0zMko=; b=Lx3R63MjhhLor1WG1sASNOTAnem7J55/I41TXQOmnnNdFEgEwrADvq8xtTYOFhInfN 6TBiL7eOjBpEZQAV0p8TosOcKXcJZ9WDuSrkve19TNDRRJqp6I76dhWbZAsft3vVp3hN J6ZufXMxLpGgAB506BhMqkBaHtC9ri+H9nU663lYl+5nOeQzgk4GcUW8MsE/T8b2hkLW 1hLzqw4ZiQjb17RIvv0DmeHsindKVijoJYyaFpgvOoOVyGCd5NN2cSqT4RQXNjDbyW1Y AqparzQ2U+ZR+ZFjOIeeusNZk47iSNe6xDg1FAAF9LC/eWKFPGRmblyToPeGLQSUnqia oHYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xKEkVXJe; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rv15si1249066ejb.364.2020.09.17.20.03.19; Thu, 17 Sep 2020 20:03:43 -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=xKEkVXJe; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730483AbgIRDBC (ORCPT + 99 others); Thu, 17 Sep 2020 23:01:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:53544 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727556AbgIRCFc (ORCPT ); Thu, 17 Sep 2020 22:05:32 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E0B0320872; Fri, 18 Sep 2020 02:05:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394731; bh=/ySKV8YltLBzBdxYlotqiaNymuxI1/8UQNeR0WNNiyo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xKEkVXJejOtG5vmAR5uJJ8goIjjPLEnxjJfHQumshSpySjZfUpUpw1ZGDO6hbAIbz tMdxNkrjLJwDp4Tsce1jCehbNLQxmC+k1bJkr3A5TZQZSsm5tsu3HvxGZc51zpyQ3b wF0Y1DuWfwhUk82qxWkPPyQXhfetcw25thzcCWDw= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Christophe JAILLET , Chuck Lever , Sasha Levin , linux-nfs@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 213/330] SUNRPC: Fix a potential buffer overflow in 'svc_print_xprts()' Date: Thu, 17 Sep 2020 21:59:13 -0400 Message-Id: <20200918020110.2063155-213-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 dc74519286be5..fe4cd0b4c4127 100644 --- a/net/sunrpc/svc_xprt.c +++ b/net/sunrpc/svc_xprt.c @@ -104,8 +104,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) { @@ -118,9 +127,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