Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1268064imu; Wed, 16 Jan 2019 16:04:01 -0800 (PST) X-Google-Smtp-Source: ALg8bN4t2Iy3J6rZFvesWWVklz3iv2+sbsCFP/L3rq9quaGy5Rdu2OZuoWrDXL9eaRKkw21JDdds X-Received: by 2002:a17:902:8e8a:: with SMTP id bg10mr12620560plb.192.1547683441355; Wed, 16 Jan 2019 16:04:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547683441; cv=none; d=google.com; s=arc-20160816; b=0Xh4y3d3tleBFXHkt1zMNyvXuBKuGIPyRbvzQ9ibYpY+b05+GIbIaYmblT+vIB45Dd rykO7yHFyr6F+Mph70sqsBBoJd2wu/xxRqAo/TZCf0YKBOWTYmkJpVlYBloccO5hAi1F A5SVXU5dqTKg1d8l9msXBS1QKnQbHYP0ScMNhJbAsCZVJeu6AdSKwViqUXoZ9/lP5qed V2nUsNDTpSh/P1Z6fZyce2eTtSFOPTUIgcOMFDfYydYi4CjGJpoH7cX5pHURWthsVVaE hSneJXZv0K6innHSwbY+BHj8AUZbifKaADuMSzZMwrPLKveqY/PWI21GP5Hj5HoD6O8J CwRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=cEnQV8MN4U/4B+Ox+HlUduKEEyFf3+VbksN6axDmC4E=; b=s+Z9zwg/MaRnwMcssaOvoJg2w7XRVWuk7psLbel3oPVq8LEfm16lrnuRpYXnBcJGC1 v22WyVVer/o3UoVGV8p6sgg1Mjjdd0qPotuO6eIE8mU4kc+slZpnUXcgf3l0pHXUJhMM AztdikLoeKcMFuv12IiMEtXLxQ1A7yrHrOQwYYG+WOUyh9vRgZQXVNw+PeJCM4KoodUp UTgYceH42iwcEuIP2zwlzPVDUZ9/5UQdd8TG7+dW4MYSUwpC0qI+MRk50nIEm1VVn7vq OxTwGZIzMoFx53Ppzrk8+Elszqdnb0zCgDaPqDMGUuRWgWgGh1Xm/Ny1EEO45CbMl5+d LF0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QCX6EvMU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f90si7750163plb.362.2019.01.16.16.03.45; Wed, 16 Jan 2019 16:04:01 -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; dkim=pass header.i=@chromium.org header.s=google header.b=QCX6EvMU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728981AbfAPSmH (ORCPT + 99 others); Wed, 16 Jan 2019 13:42:07 -0500 Received: from mail-ua1-f66.google.com ([209.85.222.66]:40650 "EHLO mail-ua1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728748AbfAPSmH (ORCPT ); Wed, 16 Jan 2019 13:42:07 -0500 Received: by mail-ua1-f66.google.com with SMTP id n7so2528313uao.7 for ; Wed, 16 Jan 2019 10:42:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cEnQV8MN4U/4B+Ox+HlUduKEEyFf3+VbksN6axDmC4E=; b=QCX6EvMUxpjSHQXqO6/ODO1oXCy1BuA11y+vMK7fiUeONPrteA53TAvOuSBsuLikLu IhUSBIU3XPO6w3BQmBFOOSu+vyUKVqUh1Wd2E9i73Dk3s2Xy7r0Urll6r7qQMK33ZIWZ Vft9KXarjIKgzQizchu9yp8VxYeMyK38438yg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cEnQV8MN4U/4B+Ox+HlUduKEEyFf3+VbksN6axDmC4E=; b=mCtN3zdSq8fcmsYLI2ucmYlQWaaTy1hlEOgyK+FcHlB1iWDxWLfXMxSw7iXStRZD/m 6XZLqU50tZrMKyPGx38dW0+ZiBwgDvsfQzj+8EZBXV/eTMMgsk9w90K2j3QQabAHyKSF nXRpagH/WPoNORkceDOnbbMXhD7MpGrvdZcLdjT30HEyXndbu8Zbs4cBhyeSZLWRJcS4 oiFBtqqSUqDRmdXxsFSsviqfOSWYV7OS/+MTWzc21hsHGCK5j2psyJUj6jur9Me/inxo KzxA7yo1fmyRyBaMdDonjO2u0gVwCFD7ByVD6sqHk/Yefwj2/caR0FgyFl6XWDe8dAoq ITDA== X-Gm-Message-State: AJcUukfN9B5ptncv5ovB1LDanpGEBUTes+P3s00t4jSEbR8Ub5sIm2rS JSBz0FkNb1l3pqtTg8sKGuevj/IGNSI= X-Received: by 2002:ab0:26cb:: with SMTP id b11mr4228008uap.112.1547664125124; Wed, 16 Jan 2019 10:42:05 -0800 (PST) Received: from mail-vk1-f177.google.com (mail-vk1-f177.google.com. [209.85.221.177]) by smtp.gmail.com with ESMTPSA id l10sm74471551vkl.54.2019.01.16.10.42.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Jan 2019 10:42:04 -0800 (PST) Received: by mail-vk1-f177.google.com with SMTP id s184so1666167vkd.6 for ; Wed, 16 Jan 2019 10:42:03 -0800 (PST) X-Received: by 2002:a1f:4982:: with SMTP id w124mr4091463vka.4.1547664122884; Wed, 16 Jan 2019 10:42:02 -0800 (PST) MIME-Version: 1.0 References: <20190112152844.26550-1-w@1wt.eu> <20190112152844.26550-6-w@1wt.eu> In-Reply-To: <20190112152844.26550-6-w@1wt.eu> From: Kees Cook Date: Wed, 16 Jan 2019 10:41:50 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 6/8] ASoC: intel: skylake: change snprintf to scnprintf for possible overflow To: Willy Tarreau Cc: Silvio Cesare , LKML , Pierre-Louis Bossart , Liam Girdwood , Jie Yang , Dan Carpenter , Will Deacon , Greg KH Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jan 12, 2019 at 7:28 AM Willy Tarreau wrote: > > From: Silvio Cesare > > Change snprintf to scnprintf. There are generally two cases where using > snprintf causes problems. > > 1) Uses of size += snprintf(buf, SIZE - size, fmt, ...) > In this case, if snprintf would have written more characters than what the > buffer size (SIZE) is, then size will end up larger than SIZE. In later > uses of snprintf, SIZE - size will result in a negative number, leading > to problems. Note that size might already be too large by using > size = snprintf before the code reaches a case of size += snprintf. > > 2) If size is ultimately used as a length parameter for a copy back to user > space, then it will potentially allow for a buffer overflow and information > disclosure when size is greater than SIZE. When the size is used to index > the buffer directly, we can have memory corruption. This also means when > size = snprintf... is used, it may also cause problems since size may become > large. Copying to userspace is mitigated by the HARDENED_USERCOPY kernel > configuration. > > The solution to these issues is to use scnprintf which returns the number of > characters actually written to the buffer, so the size variable will never > exceed SIZE. > > Signed-off-by: Silvio Cesare > Cc: Pierre-Louis Bossart > Cc: Liam Girdwood > Cc: Jie Yang > Cc: Dan Carpenter > Cc: Kees Cook > Cc: Will Deacon > Cc: Greg KH > Signed-off-by: Willy Tarreau > > --- > sound/soc/intel/skylake/skl-debug.c | 28 ++++++++++++++-------------- > 1 file changed, 14 insertions(+), 14 deletions(-) > > diff --git a/sound/soc/intel/skylake/skl-debug.c b/sound/soc/intel/skylake/skl-debug.c > index 5d7ac2ee7a3c..bb28db734fb7 100644 > --- a/sound/soc/intel/skylake/skl-debug.c > +++ b/sound/soc/intel/skylake/skl-debug.c > @@ -43,7 +43,7 @@ static ssize_t skl_print_pins(struct skl_module_pin *m_pin, char *buf, > ssize_t ret = 0; > > for (i = 0; i < max_pin; i++) > - ret += snprintf(buf + size, MOD_BUF - size, > + ret += scnprintf(buf + size, MOD_BUF - size, > "%s %d\n\tModule %d\n\tInstance %d\n\t" > "In-used %s\n\tType %s\n" > "\tState %d\n\tIndex %d\n", > While working on a Coccinelle script to find more cases of this, I noticed that this code is buggy: it keeps overwriting the same position in the buf string: "buf + size" and don't take "ret" into account at all. This needs to be: ret += scnprintf(buf + size + ret, MOD_BUF - size - ret, -- Kees Cook