From patchwork Tue Feb 20 13:09:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 128903 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp4622050ljc; Tue, 20 Feb 2018 05:09:50 -0800 (PST) X-Google-Smtp-Source: AH8x225dqIxb2RZIEngobRFt1Ui7cXQkIwErpEHpeV10k0eVJYF0TlxPZ4gt/tDp7PG7HiTGxH9D X-Received: by 10.98.202.138 with SMTP id y10mr6682739pfk.184.1519132190678; Tue, 20 Feb 2018 05:09:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519132190; cv=none; d=google.com; s=arc-20160816; b=OcaSkv7ydDkeQCltuEwkRiiHMOyLFyS1+iMEYeKvcuMLsTBzc5yVutUEdDQZB2P8Tn n7zkmVQap5LTxJGSDExNcFxNmb8DuFsa7R2jogV8W8c8aJ4f0u4qNG/FZlH1C+Tf6ezS voYHFkGLmwFpGgBPYjvH7zBwchniQ5Hcg7oWwGIJ84eyHW2wrDG971/W5viN+mgGT+q1 Audb+VLFyBETUgLacyataxqtrEu7+Qz+U4dMSjSGpjFKBBHsEskWE3fDgA+18WP7KVVP HB/QK4suqnMeCaJunft7BtYwyqsScCG3M4V2MMTwXI5JV7S38PsT8Bkm4/8slZeLw+sM NQ7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=52uuc/dBeRH1/Ud6uGxokCGukLvPcEho4YX03MF1r1k=; b=cfFFPrQ0oQDGOdH8jvrE3uRF7auALKlDHOFPAY/5Vf5xPF+K3gsz1RdPkpnxwt2Wjw nInTMBJMPy4UsBRcpHzVpzWC63fRunHGgdn0/GcNFbM4vbVk4aREq+mKtv2d4EhT1/Nr G8yL5DROgLEHOWbIIlEm+6oKQC9Y2xegCRRMMG1Z48CbOxg8O/3/Qf8qTbS00snsOSb0 OAV0mn3bkBmRC6NQD25CSAkki+LQhLTTVPh1PhKAQsEM4trX9wn5N4KKxghY8k27zpMT Hn9nZIXOOeZzTSiuNNk7CW/V6tzpuDxkT/Qxhja2jSMbQkCL+FRsKyQD1YQ7ETynMEH/ MBtA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r7si304380pfg.30.2018.02.20.05.09.50; Tue, 20 Feb 2018 05:09:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751895AbeBTNJq (ORCPT + 28 others); Tue, 20 Feb 2018 08:09:46 -0500 Received: from mout.kundenserver.de ([212.227.126.134]:36153 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751765AbeBTNJo (ORCPT ); Tue, 20 Feb 2018 08:09:44 -0500 Received: from wuerfel.lan ([95.208.111.237]) by mrelayeu.kundenserver.de (mreue001 [212.227.15.129]) with ESMTPA (Nemesis) id 0Llv9N-1eFL5L25IX-00Zisp; Tue, 20 Feb 2018 14:09:26 +0100 From: Arnd Bergmann To: Shaohua Li Cc: Arnd Bergmann , NeilBrown , Artur Paszkiewicz , Jens Axboe , Song Liu , Guoqing Jiang , linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] md: raid5: avoid string overflow warning Date: Tue, 20 Feb 2018 14:09:11 +0100 Message-Id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Yno76pL3 +ViOKyXcCa97KzFDxkFsI0+Rum7LH2TKbj4NMw== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org gcc warns about a possible overflow of the kmem_cache string, when adding four characters to a string of the same length: drivers/md/raid5.c: In function 'setup_conf': drivers/md/raid5.c:2207:34: error: '-alt' directive writing 4 bytes into a region of size between 1 and 32 [-Werror=format-overflow=] sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); ^~~~ drivers/md/raid5.c:2207:2: note: 'sprintf' output between 5 and 36 bytes into a destination of size 32 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If I'm counting correctly, we need 11 characters for the fixed part of the string and 18 characters for a 64-bit pointer (when no gendisk is used), so that leaves three characters for conf->level, which should always be sufficient. This makes the code use snprintf() with the correct length, to make the code more robust against changes, and to get the compiler to shut up. In commit f4be6b43f1ac ("md/raid5: ensure we create a unique name for kmem_cache when mddev has no gendisk") from 2010, Neil said that the pointer could be removed "shortly" once devices without gendisk are disallowed. I have no idea if that happened, but if it did, that should probably be changed as well. Signed-off-by: Arnd Bergmann --- drivers/md/raid5.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) -- 2.9.0 diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index 50d01144b805..7ef368061424 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -2196,15 +2196,16 @@ static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) static int grow_stripes(struct r5conf *conf, int num) { struct kmem_cache *sc; + size_t namelen = sizeof(conf->cache_name[0]); int devs = max(conf->raid_disks, conf->previous_raid_disks); if (conf->mddev->gendisk) - sprintf(conf->cache_name[0], + snprintf(conf->cache_name[0], namelen, "raid%d-%s", conf->level, mdname(conf->mddev)); else - sprintf(conf->cache_name[0], + snprintf(conf->cache_name[0], namelen, "raid%d-%p", conf->level, conf->mddev); - sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); + snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]); conf->active_name = 0; sc = kmem_cache_create(conf->cache_name[conf->active_name],