Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2973071pxb; Mon, 18 Apr 2022 12:20:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzl54BGKfcaOfr4le8VmDQPKb718kdlkJ3fkvCql+U3NjXqpHn7n7nR/uiEoPs4+34zdntG X-Received: by 2002:aa7:9472:0:b0:508:44db:be37 with SMTP id t18-20020aa79472000000b0050844dbbe37mr14004806pfq.25.1650309644298; Mon, 18 Apr 2022 12:20:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650309644; cv=none; d=google.com; s=arc-20160816; b=k/OvaOIHgw/+SdZS3jrZi4g23jtGRC0E7KvkQ8X6fOjvfNxuOojHiVoaMrN2sdvrkM D6d76nED3uDYzptXb0Nt/j0UtXV7Xy3oFl+qtFnYj/dotVPShJbArbqOchQqekhOOzFk u1USleyVeDX480VoJsToxPRbJgxkHfWf03jg/qkbmOgvAa4/ez6aXyov6pjCUqD9pGrq H6Yi+BhmTQEzv3G6dewLS2TxtfhE0PsjRiSdm6TjsKCPs+VL+LFdjmld9QU7M8ilo702 CnoKQEMGIJM6U+J9iWytvAdX/l2z/qpQccco/lCu25ThKDpPVZ82z2KOVl5zs9uyFWCK jFjg== 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=e6aanR29DII2HiUyORIPIK5DJJ7LsJZOVwho3f3W46A=; b=fKSUzMrGzQzoACDvOw1DUV2/YSfSzHCk+6tQ2g/BHWOyFMfykxsLlwpSlPjFdLvrcj kZn/RTfnk8C/HoEzJtBG3d25RPy6eOtkET1YP907nn7qkccD4iiOcQoNoS6R8PVpl+cF CAyA8yYXtanOkzPNOI95egZT31T5XBOtfb1apqVoJhChA8U5hmVhIAvJkAe5Uu5Btiy5 zq3xxCLHPtj5mMN8eGJFla29YVD7moz7HS93hjszlzc87UowjjgAW/sG29kMl4xqQlV2 8t+ki59dLx6afJJNLEu6QEdRvyoe6RWxuH7n4E3kdfkoa7esaGal7qvY68IEhyLSYszP xZnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Pk/eRWSo"; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f3-20020a170902860300b00153b974f3aesi9044706plo.427.2022.04.18.12.20.28; Mon, 18 Apr 2022 12:20:44 -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=@linuxfoundation.org header.s=korg header.b="Pk/eRWSo"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243181AbiDRNj6 (ORCPT + 99 others); Mon, 18 Apr 2022 09:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240072AbiDRNMC (ORCPT ); Mon, 18 Apr 2022 09:12:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFC4E21251; Mon, 18 Apr 2022 05:50:46 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 63F6F6124E; Mon, 18 Apr 2022 12:50:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62722C385A1; Mon, 18 Apr 2022 12:50:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286245; bh=zEeKefMZYRslSx40JLS5O+6+t08QoeqPa7ZuL/Iyqg4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pk/eRWSoUA3Wysw3T5ivxjULZYHbc6g9F/CYtBnjL8Iva+XihOiXELYxiVCgoaExV reIoTxTMl0VQe5OwhwcZTpYNl9RzIwh2yOggicGIS2kNMhE4cXl537KPDOcSTDo+hU r1ptq8VC5cYqFrx0jNDPqSXU+9D3VSL/kynmUAn0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Helge Deller , Sasha Levin Subject: [PATCH 4.14 073/284] video: fbdev: fbcvt.c: fix printing in fb_cvt_print_name() Date: Mon, 18 Apr 2022 14:10:54 +0200 Message-Id: <20220418121212.763660563@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Dan Carpenter [ Upstream commit 78482af095abd9f4f29f1aa3fe575d25c6ae3028 ] This code has two bugs: 1) "cnt" is 255 but the size of the buffer is 256 so the last byte is not used. 2) If we try to print more than 255 characters then "cnt" will be negative and that will trigger a WARN() in snprintf(). The fix for this is to use scnprintf() instead of snprintf(). We can re-write this code to be cleaner: 1) Rename "offset" to "off" because that's shorter. 2) Get rid of the "cnt" variable and just use "size - off" directly. 3) Get rid of the "read" variable and just increment "off" directly. Fixes: 96fe6a2109db ("fbdev: Add VESA Coordinated Video Timings (CVT) support") Signed-off-by: Dan Carpenter Signed-off-by: Helge Deller Signed-off-by: Sasha Levin --- drivers/video/fbdev/core/fbcvt.c | 53 +++++++++++++------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/drivers/video/fbdev/core/fbcvt.c b/drivers/video/fbdev/core/fbcvt.c index 55d2bd0ce5c0..64843464c661 100644 --- a/drivers/video/fbdev/core/fbcvt.c +++ b/drivers/video/fbdev/core/fbcvt.c @@ -214,9 +214,11 @@ static u32 fb_cvt_aspect_ratio(struct fb_cvt_data *cvt) static void fb_cvt_print_name(struct fb_cvt_data *cvt) { u32 pixcount, pixcount_mod; - int cnt = 255, offset = 0, read = 0; - u8 *buf = kzalloc(256, GFP_KERNEL); + int size = 256; + int off = 0; + u8 *buf; + buf = kzalloc(size, GFP_KERNEL); if (!buf) return; @@ -224,43 +226,30 @@ static void fb_cvt_print_name(struct fb_cvt_data *cvt) pixcount_mod = (cvt->xres * (cvt->yres/cvt->interlace)) % 1000000; pixcount_mod /= 1000; - read = snprintf(buf+offset, cnt, "fbcvt: %dx%d@%d: CVT Name - ", - cvt->xres, cvt->yres, cvt->refresh); - offset += read; - cnt -= read; + off += scnprintf(buf + off, size - off, "fbcvt: %dx%d@%d: CVT Name - ", + cvt->xres, cvt->yres, cvt->refresh); - if (cvt->status) - snprintf(buf+offset, cnt, "Not a CVT standard - %d.%03d Mega " - "Pixel Image\n", pixcount, pixcount_mod); - else { - if (pixcount) { - read = snprintf(buf+offset, cnt, "%d", pixcount); - cnt -= read; - offset += read; - } + if (cvt->status) { + off += scnprintf(buf + off, size - off, + "Not a CVT standard - %d.%03d Mega Pixel Image\n", + pixcount, pixcount_mod); + } else { + if (pixcount) + off += scnprintf(buf + off, size - off, "%d", pixcount); - read = snprintf(buf+offset, cnt, ".%03dM", pixcount_mod); - cnt -= read; - offset += read; + off += scnprintf(buf + off, size - off, ".%03dM", pixcount_mod); if (cvt->aspect_ratio == 0) - read = snprintf(buf+offset, cnt, "3"); + off += scnprintf(buf + off, size - off, "3"); else if (cvt->aspect_ratio == 3) - read = snprintf(buf+offset, cnt, "4"); + off += scnprintf(buf + off, size - off, "4"); else if (cvt->aspect_ratio == 1 || cvt->aspect_ratio == 4) - read = snprintf(buf+offset, cnt, "9"); + off += scnprintf(buf + off, size - off, "9"); else if (cvt->aspect_ratio == 2) - read = snprintf(buf+offset, cnt, "A"); - else - read = 0; - cnt -= read; - offset += read; - - if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK) { - read = snprintf(buf+offset, cnt, "-R"); - cnt -= read; - offset += read; - } + off += scnprintf(buf + off, size - off, "A"); + + if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK) + off += scnprintf(buf + off, size - off, "-R"); } printk(KERN_INFO "%s\n", buf); -- 2.34.1