Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp11270086ybi; Thu, 25 Jul 2019 13:06:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqzofRwplvZAb3U88HutCIGJHD4yLSIvRZNGwesVV1xAQ9jnBvSk5Iv4utb525i6/Mjpq6UG X-Received: by 2002:a17:902:820c:: with SMTP id x12mr93381368pln.216.1564085177372; Thu, 25 Jul 2019 13:06:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564085177; cv=none; d=google.com; s=arc-20160816; b=MeDopKbFJLdIshq93sJ+meFNm/40VqKRdB0GH+/cbr/HmDvkAAlMoBlx8fbpTpqwt+ 9On1qqnRX6qFnIav5jtz1g4zNHiwP6vHs/kdmdj250oZdgbrSQD1o1GAo5SC9bm24E0h TSE4GFrU+qurDXZ2Q7Nt6CQPpcYRR1O+Gurt3hSlcj4UXyT1ELdVbVu8RJFh8/BNXvWc gxL/V107yVjrbbvTMTiCbFytNiib5hFA2TsKy8E+bemahF9thiLcK6YWUEKSahP0k4Wc f3Jh+4hbn7f2+3DxaJlKfTiIFwNisseEb+eL2tRI7ENdTUGm393+J7WQxNhtN67IL8qV MCUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=uHw7zEXiXiWuFCeMIM1DoPEM5lUo8QWt7fmyIitbzd4=; b=fHnmQScAD7S7D44DqFdkTyK4U2lTEBJoo54MMDdNDfwXdB2k7epUwkyCmOMmoibxsf GTj5/hRkr7E+RCJUOI/dOd2HQNmo+5+4GWdYls7FIygGtfD28ELOqVAfulDPPb8/PMuW 4F2ApR1yZQhij2AYQ05nYV5zro6mXESP7SRyTTVg++N9TF4Cb+Q966yfp5+8hhZrfFTl b0ouem0c5uOxKf9QyFnd896FnSNUi/kGP7UzuOLEGSoIV0O2awOf2MAONcUa/vVWuIiO x8uPi9gEH9NMRpi5h80ghofZ+iEtVvTE7xJxTGg+CUaB2Y7Qe2zObfkPlD3cNiq9izeB Hgwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gRrhkgmX; 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 h129si19849536pgc.508.2019.07.25.13.06.01; Thu, 25 Jul 2019 13:06:17 -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=@chromium.org header.s=google header.b=gRrhkgmX; 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 S1726715AbfGYUDd (ORCPT + 99 others); Thu, 25 Jul 2019 16:03:33 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:34097 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726585AbfGYUDd (ORCPT ); Thu, 25 Jul 2019 16:03:33 -0400 Received: by mail-pf1-f195.google.com with SMTP id b13so23284029pfo.1 for ; Thu, 25 Jul 2019 13:03:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=uHw7zEXiXiWuFCeMIM1DoPEM5lUo8QWt7fmyIitbzd4=; b=gRrhkgmXX57rpW/eOfU07DHgYIRJaSRHWUMLcqkuazmJ9eSgJ3ejVn/JebuAtKVADk Es6uzHuBitkmgkTqzHq1W3PJ0BRywQ0CY27gZ/QC9xT0zl4goLkz+41D93WnbaD1uMqw G/fn+b5k6gja9lg/U079hwILSubBD+sUO58DQ= 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; bh=uHw7zEXiXiWuFCeMIM1DoPEM5lUo8QWt7fmyIitbzd4=; b=WC9jtQvMhw2pHOFzAiV2dQ0dzBVlCPLvnrmPH0doer43T2avmKM59ife63j8sFE03z yW28s738ECGJ9GX/QjTRVE/4F17HX4wW/ARZhnp6d4kwa/VrwgLKfxM08n3cqqbhDcKu X/nwOYPlDUVPbyLeSLmt3boxSJc2REJod+08UpA6FvSol+VWApFwrwYf0tGstz3+J1HD 3v4kOnrQHNlLnMglBlWig4VY4TNdYNIE8GZajk+FE5ekUIGtDTLnuc8M9xzfwjkF32Hb ndmXPL0ndybM2p6Jl5BRh5J/cOiQ97h/XhDKcZcW7yMR08Z4mIdXU1zapThBUpP7s8Pi B7eg== X-Gm-Message-State: APjAAAUQJwf3Sn/d1pde1xEtyE1Ognkb/JMa1VTp6uS1gbONLBUCbgbK DoZEjZaaBswW0YZTBnafMy3+Rg8Sr5c= X-Received: by 2002:a63:a302:: with SMTP id s2mr18456663pge.125.1564085012343; Thu, 25 Jul 2019 13:03:32 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p65sm50026998pfp.58.2019.07.25.13.03.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 25 Jul 2019 13:03:31 -0700 (PDT) Date: Thu, 25 Jul 2019 13:03:30 -0700 From: Kees Cook To: Linus Torvalds Cc: Rasmus Villemoes , Yann Droneaud , David Laight , Joe Perches , "linux-kernel@vger.kernel.org" , Jonathan Corbet , Stephen Kitt , Nitin Gote , "jannh@google.com" , "kernel-hardening@lists.openwall.com" , Andrew Morton Subject: Re: [PATCH 1/2] string: Add stracpy and stracpy_pad mechanisms Message-ID: <201907251301.E1E32DCCCE@keescook> References: <7ab8957eaf9b0931a59eff6e2bd8c5169f2f6c41.1563841972.git.joe@perches.com> <5ffdbf4f87054b47a2daf23a6afabecf@AcuMS.aculab.com> <396d1eed-8edf-aa77-110b-c50ead3a5fd5@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 24, 2019 at 10:08:57AM -0700, Linus Torvalds wrote: > On Wed, Jul 24, 2019 at 6:09 AM Rasmus Villemoes > wrote: > > > > The kernel's snprintf() does not behave in a non-standard way, at least > > not with respect to its return value. > > Note that the kernels snprintf() *does* very much protect against the > overflow case - not by changing the return value, but simply by having > > /* Reject out-of-range values early. Large positive sizes are > used for unknown buffer sizes. */ > if (WARN_ON_ONCE(size > INT_MAX)) > return 0; > > at the very top. > > So you can't actually overflow in the kernel by using the repeated > > offset += vsnprintf( .. size - offset ..); > > model. > > Yes, it's the wrong thing to do, but it is still _safe_. Actually, perhaps we should add this test to strscpy() too? diff --git a/lib/string.c b/lib/string.c index 461fb620f85f..0e0d7628ddc4 100644 --- a/lib/string.c +++ b/lib/string.c @@ -182,7 +182,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count) size_t max = count; long res = 0; - if (count == 0) + if (count == 0 || count > INT_MAX) return -E2BIG; #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -- Kees Cook