From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BE04C3DA7A for ; Thu, 5 Jan 2023 21:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235284AbjAEVQw (ORCPT ); Thu, 5 Jan 2023 16:16:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230051AbjAEVQr (ORCPT ); Thu, 5 Jan 2023 16:16:47 -0500 Received: from mx01.omp.ru (mx01.omp.ru [90.154.21.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 330701182D for ; Thu, 5 Jan 2023 13:16:42 -0800 (PST) Received: from [192.168.1.103] (178.176.78.185) by msexch01.omp.ru (10.188.4.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.986.14; Fri, 6 Jan 2023 00:16:31 +0300 To: Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes From: Sergey Shtylyov Subject: [PATCH] vsprintf: fix possible NULL pointer deref in vsnprintf() Organization: Open Mobile Platform CC: Message-ID: <1f4d159e-5382-3c75-bd5e-42337ecd8c28@omp.ru> Date: Fri, 6 Jan 2023 00:16:31 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [178.176.78.185] X-ClientProxiedBy: msexch01.omp.ru (10.188.4.12) To msexch01.omp.ru (10.188.4.12) X-KSE-ServerInfo: msexch01.omp.ru, 9 X-KSE-AntiSpam-Interceptor-Info: scan successful X-KSE-AntiSpam-Version: 5.9.59, Database issued on: 01/05/2023 20:33:33 X-KSE-AntiSpam-Status: KAS_STATUS_NOT_DETECTED X-KSE-AntiSpam-Method: none X-KSE-AntiSpam-Rate: 59 X-KSE-AntiSpam-Info: Lua profiles 174536 [Jan 05 2023] X-KSE-AntiSpam-Info: Version: 5.9.59.0 X-KSE-AntiSpam-Info: Envelope from: s.shtylyov@omp.ru X-KSE-AntiSpam-Info: LuaCore: 502 502 69dee8ef46717dd3cb3eeb129cb7cc8dab9e30f6 X-KSE-AntiSpam-Info: {rep_avail} X-KSE-AntiSpam-Info: {Tracking_from_domain_doesnt_match_to} X-KSE-AntiSpam-Info: {relay has no DNS name} X-KSE-AntiSpam-Info: {SMTP from is not routable} X-KSE-AntiSpam-Info: {Found in DNSBL: 178.176.78.185 in (user) b.barracudacentral.org} X-KSE-AntiSpam-Info: {Found in DNSBL: 178.176.78.185 in (user) dbl.spamhaus.org} X-KSE-AntiSpam-Info: 127.0.0.199:7.1.2;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;omp.ru:7.1.1 X-KSE-AntiSpam-Info: ApMailHostAddress: 178.176.78.185 X-KSE-AntiSpam-Info: {DNS response errors} X-KSE-AntiSpam-Info: Rate: 59 X-KSE-AntiSpam-Info: Status: not_detected X-KSE-AntiSpam-Info: Method: none X-KSE-AntiSpam-Info: Auth:dmarc=temperror header.from=omp.ru;spf=temperror smtp.mailfrom=omp.ru;dkim=none X-KSE-Antiphishing-Info: Clean X-KSE-Antiphishing-ScanningType: Heuristic X-KSE-Antiphishing-Method: None X-KSE-Antiphishing-Bases: 01/05/2023 20:37:00 X-KSE-AttachmentFiltering-Interceptor-Info: protection disabled X-KSE-Antivirus-Interceptor-Info: scan successful X-KSE-Antivirus-Info: Clean, bases: 1/5/2023 6:02:00 PM X-KSE-BulkMessagesFiltering-Scan-Result: InTheLimit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In vsnprintf() etc, C99 allows the 'buf' argument to be NULL when the 'size' argument equals 0. Let us treat NULL passed as if the 'buf' argument pointed to a 0-sized buffer, so that we can avoid a NULL pointer dereference and still return the # of characters that would be written if 'buf' pointed to a valid buffer... Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool. Signed-off-by: Sergey Shtylyov --- This patch is against the 'master' branch of the PRINTK Group's repo... lib/vsprintf.c | 9 +++++++++ 1 file changed, 9 insertions(+) Index: linux/lib/vsprintf.c =================================================================== --- linux.orig/lib/vsprintf.c +++ linux/lib/vsprintf.c @@ -2738,6 +2738,15 @@ int vsnprintf(char *buf, size_t size, co if (WARN_ON_ONCE(size > INT_MAX)) return 0; + /* + * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if + * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer + * dereference and still return # of characters that would be written + * if @buf pointed to a valid buffer... + */ + if (!buf) + size = 0; + str = buf; end = buf + size;