Received: by 10.213.65.68 with SMTP id h4csp184708imn; Thu, 15 Mar 2018 22:55:27 -0700 (PDT) X-Google-Smtp-Source: AG47ELswR0qH/aJfJXPZ3sEJbMCxN4yePUdGkPN6kpHjmPV38ajjrCOxoFl75Ln4wicZLaEBuAgF X-Received: by 10.98.144.65 with SMTP id a62mr538869pfe.96.1521179726991; Thu, 15 Mar 2018 22:55:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521179726; cv=none; d=google.com; s=arc-20160816; b=G/U7fiUwG20Wi/JJIAO5zlsbA7NWOLKcEHXNfMWZr1gzT5v9JdBREe6Sb0qbCARKGd bLYCI9DaP5D5z8s15Ssxwx9QmW1zr0Gvgnl2E2RiV+y0YNExQV6JF0nefrxX0LOo5Iq2 XmrPNjvHKb5AbtCYK7+I5F18dvZp5qDe8542BiHpMhwTEN73AzaPw/k3lLOBdv/SE6TU 46yVazgi6bXOOjZSywm5INmC/uxTi7uI5bIR2T0CMM6ES4Z0ImlvgtTpm0wt10Y68B/m zFqRqz1UK5w2+b4etW/PGrWNYkqXEU4z6T0gLBpBOjefwudWe3oE+UUpjfKuQ19WidDF t2bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=CRHWZhvb4PBh5jzPAlaqCIKVEjaVl+WjtGAhS9EKhtI=; b=yW7DtVbrYf20uVDPfrZ6HlO2qqEM2DcpDeeOqbb1lpbo/e9+oL/PzzbrfVZO8RSNfb RJ9SkNwzB04l4EIWc5cTBz1ih4RpKKu6nDvXl8ddwzFLLe3AqFNRCjw4mL7tenphGLig /jheb32+Dy88I/86/lLhw6cFaMfUfcrC5YHEpFptxyg/OS2T5+oe4xNTFc2oCgNwk8bM EX7GgddkQubexkNkISu84oDdjIwm/v5uQXsNWvaRxxLNZzlNNoUbSGNFvCRkbcrb49Ex 6+xurjjbDX7q0ribVLgZ+7uQgt/n71bk8R2oTlAixJQWYY7Vmxf6r2+scIRGMmVFlS16 ywjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Cb3wt8Z5; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j61-v6si5648286plb.395.2018.03.15.22.55.13; Thu, 15 Mar 2018 22:55:26 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=Cb3wt8Z5; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752897AbeCPFxw (ORCPT + 99 others); Fri, 16 Mar 2018 01:53:52 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:41556 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750767AbeCPFxv (ORCPT ); Fri, 16 Mar 2018 01:53:51 -0400 Received: by mail-pl0-f65.google.com with SMTP id b7-v6so1245982plr.8 for ; Thu, 15 Mar 2018 22:53:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=CRHWZhvb4PBh5jzPAlaqCIKVEjaVl+WjtGAhS9EKhtI=; b=Cb3wt8Z5r5XdxHPPyBgJtY+DYf6NVM+Iwo4G1RS/KxjbKV9fUol8LMJzY3g86q4Dsf kKFeV+H33azSlnxCt4zju/xxSH7dBREoQxIkeIsKbjRlALv0Xf1IDO+bsiHhTMqSs7sw h4pG00ZM3Avbd+e3Ngd2VzyNOTadmdVIpfJaV0eCYCZ3auAW2+fkOIL2qd9WuQPn67xT ZJKLfyv7iN+H5gYgT8YnZFU9ffLxuTwOPM38oVsP1gAUyT+UHlO8CeVSSyWH63UDtnmq 26Ar0Em7RT6xDIVDti+QEcJv9TDG5BPohJCBzF6fe9Li2dmLlrLgDomF+cp0kVajB4N3 uAmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=CRHWZhvb4PBh5jzPAlaqCIKVEjaVl+WjtGAhS9EKhtI=; b=YbQ0HY8q5Ogj7cs6CekNSO9smV5lpRRMbLurnZc/R39kDLOTvnlS5rZ83am2ADM5ac 0/m8ZvOZ94d041c133qhOA9UEyf4AaL09s46eV/+f4tVxwHKuTJtvSP/41Kq5I+IhmhA RuN8YvOEpXI2Z2hALaqohekcgLTOy0PdJmYAX8P+0M72+JFrGGa15esChtzFc0yHxaZL Gl9KcLvQG+oWRRa+qEuLYnBEIVMP2iVDcMJ3apBEY14XdQTj8Zl69EZLo+7ZbUpDnJQp chwIp3ZoJeUtYlc9DQBcID+nlG9mkknBmdcJ5rjwFGHQ5FB3zrXL1FNOWApXXYkWEnpK iDbg== X-Gm-Message-State: AElRT7FqmMw14AGG/uMtq6m6DAs3mnT0W3PXxGnskcHqUHEh5w8h3B6V K78WtQ+xBOdmJWfJIb08IwU= X-Received: by 2002:a17:902:102:: with SMTP id 2-v6mr734899plb.48.1521179630772; Thu, 15 Mar 2018 22:53:50 -0700 (PDT) Received: from localhost ([39.7.47.97]) by smtp.gmail.com with ESMTPSA id y3sm11492863pgc.81.2018.03.15.22.53.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 15 Mar 2018 22:53:49 -0700 (PDT) Date: Fri, 16 Mar 2018 14:53:46 +0900 From: Sergey Senozhatsky To: Linus Torvalds , Petr Mladek , Steven Rostedt Cc: Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , "Tobin C . Harding" , Joe Perches , Linux Kernel Mailing List , Andrew Morton , Michal Hocko , Sergey Senozhatsky Subject: Re: [PATCH v3] vsprintf: Prevent crash when dereferencing invalid pointers Message-ID: <20180316055346.GB5139@jagdpanzerIV> References: <20180309150153.3sxbbpd6jdn2d5yy@pathway.suse.cz> <20180314140947.rs3b6i5gguzzu5wi@pathway.suse.cz> <20180315075842.GD3628@jagdpanzerIV> <20180315080309.GF3628@jagdpanzerIV> <20180315130117.7c2fb761@vmware.local.home> <20180316011852.GA5139@jagdpanzerIV> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On (03/15/18 18:35), Linus Torvalds wrote: > On Thu, Mar 15, 2018 at 6:18 PM, Sergey Senozhatsky > wrote: > > > > Hm, may be sizeof(ptr) still won't suffice. It would be great if we > > could always look at spec.field_width, which can be up to 2 * sizeof(void *), > > and then just probe_kernel_read(spec.field_width). E.g., %b/%bl prints out a > > bitmap, accessing max_t(int, spec.field_width, 0) bits, which is good. But, > > for instance, %U (uuid printout) doesn't look at spec.field_width, and reads > > in 16 bytes from the given memory address. Then we have ipv4/ipv6, mac, etc. > > So I think that checking just 1 byte or sizeof(ptr) is not really enough if > > we want to fix vsprintf. What do you think? > > Honestly, I think it would be better to move the whole logic to the > functions that actually do the printout. > > Then you can do it right, and you don't need to have the strchr() either. > > There really isn't any commonality between the different versions. > field_width is meaningless, since it's about the size of the _printed_ > field, not the size in memory. Agreed! > Would it be a few more lines? Yes. But it would also clarify the code > and get all the cases right. Look at hex_string() for example, and > imagine fetching a byte at a time and just getting the corner cases > automatically right. So, basically, what I tried to say - any byte past the first sizeof(ptr) bytes or past the first byte that we check_access() can cause problems, which this patch is trying to address. As an example, FORMAT_TYPE_STR case printk("%.*s\n", p->buf) vsnprintf() string() Where ->buf is a _nearly always_ properly nul terminated char buf[128] array in struct foo. So moving that check_access() to every function that does printout sounds good to me, as well as checking every byte we access [assuming that we want to cure vsprintf], not just the first one or the first sizeof(ptr) bytes. -ss