Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp7469817imm; Tue, 28 Aug 2018 12:37:50 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYYaYVYHj9IxGvhrTTxj7DRsmHL6nblNYy7NReY4zeupclJTWVXC/kh1oP+C082iFdQnucx X-Received: by 2002:a63:8b44:: with SMTP id j65-v6mr2798642pge.325.1535485069964; Tue, 28 Aug 2018 12:37:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535485069; cv=none; d=google.com; s=arc-20160816; b=pep8uzAb+IBme0v0Bxwf7/v5uV14QV/KdAEIUikDwGZowoLtDqBmitU20Pg/fftFS1 0qzJkvwQzYWlwBnXw3ytQHbTOqodGCNEftBZ7wzlK9fQaYx22gmVGbkQpPOGtByxPzPB 0/Wmtv1ydf3JDerbuCMa4YwF1+cbi3dpK1MaRIZJ4Ji2gd8Or45ryHVmFeKTzfUdKoQB kvvoM3cWDiXIgasS1EtYP6sC1+gXVWb99cc38hYVWwsB2K3k8Ixz79iKbolapqgPjJ06 YpIcuaWnuSg6zNPNPQmnSM4U8CMqPLkmqjyHoufSOYd7vf1dZdF7MT1NbmCBwruy940w LCKQ== 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=AeeABSgOQZA+mcEvaLBu4nLj5jhPDAhkaHbFkjDsJdQ=; b=DP/t/AVO8z/6+XvwflfQUGfRiHAPfHc4DzSrgIwazckDwNNgC2u/jZTOJrLlyaAYno gbL6tw0fN49sqglCXz4fATlCckLJojlzUJK8NnSNjBItOOOCDYzQl3FoHTPdgbHE7z5V RYw72MeWMQ5DZF1coCf1lTFLxrMNrkD9L4oB4Ki0zVMTRzWTCTn/WUKRH3PR2wQBAnUf k/AF0C33au8hX/v2FsXzuamjMm6HbW5ld0fqvp3Y1gt4dYm02Vpa67vQDXhDmGffxLRP v8s9i0N6KvQLAZbzEa3h6+L17KqCMmgiFeWt+iRwGMJo3c8d10HEBMKL7ONOI6xxRjyI TDyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=IinMVC6H; 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 7-v6si1764774pgw.207.2018.08.28.12.37.34; Tue, 28 Aug 2018 12:37:49 -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=IinMVC6H; 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 S1727379AbeH1X2N (ORCPT + 99 others); Tue, 28 Aug 2018 19:28:13 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:45206 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727130AbeH1X2M (ORCPT ); Tue, 28 Aug 2018 19:28:12 -0400 Received: by mail-ed1-f65.google.com with SMTP id p52-v6so2205380eda.12 for ; Tue, 28 Aug 2018 12:35:05 -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=AeeABSgOQZA+mcEvaLBu4nLj5jhPDAhkaHbFkjDsJdQ=; b=IinMVC6HCi0U/Lm10MBYofmHBGn99Q+POtI0I6bcW01HizawPIQg+olJZwacxGKCtY 363F+cJEctPxItS4czOR3snPeRp4mJo9/HGMDCpdu68oxnbd1cNb6H209+D70j3IseYp /KACzDbj67J4YGXTAQyomcaVyXaV5sXXq8/oMoch1dfEu7R25NDgiJzoHs7A8MDvwYLV BdjR+sN+SWotg7IEYQ+yrGHupdzaHVeb+u4tK4jryfdBdUp5Kx+wRy4/qYRU5E15CO4m SpLOLgoVPqJymysdTa++FP2ssc3Q5T9NwVqhK6+isw/4ImQ6UXMbFA8QBlQ7XfzxW8yQ 4s0A== 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=AeeABSgOQZA+mcEvaLBu4nLj5jhPDAhkaHbFkjDsJdQ=; b=az2pWAEnxpDyVZ0cm0kA5xZUUzD5CTjolgYm7YfI4IZxytiC8ScAZYlbQclEG19hSK TmcJkhhEK2+uYv+6hA1hK1OaecIsPtlq+YK8mLe4tGtciT5hLGsw2ZAKR6mf7Ktc8v8K b94sbFnkOmfaFizFihl0ypHGzJUn2bu+mRRbr1cydu+gAnNh9of+Qpwb2dTc33qZBNfg gn/2jZiNjDoiqead9dashtlEAbr1xbmPkcusVuSoOlJOmJW17PPc/ioeDU6dpBje12FB LnvstGTAZg2vOoMFP3iet0qYe/LeJse4BrNeV5G2mMTIjQ1n6gTVKgTAD6+25EeCMWaO Equw== X-Gm-Message-State: APzg51Bg809cwGmMkkyQfSqPEikX2LdEsmCBR2Q+MLCeIWRPFICVDtRb CF8cabDNc0mvhM1e+ZsaclZ6FV0= X-Received: by 2002:a50:f69b:: with SMTP id d27-v6mr4073652edn.103.1535484904904; Tue, 28 Aug 2018 12:35:04 -0700 (PDT) Received: from avx2 (nat4-minsk-pool-46-53-177-92.telecom.by. [46.53.177.92]) by smtp.gmail.com with ESMTPSA id b9-v6sm1005249edk.62.2018.08.28.12.35.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Aug 2018 12:35:04 -0700 (PDT) Date: Tue, 28 Aug 2018 22:35:02 +0300 From: Alexey Dobriyan To: "Ahmed S. Darwish" Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 11/13] proc: readdir /proc/*/task Message-ID: <20180828193502.GB4847@avx2> References: <20180827231503.26899-1-adobriyan@gmail.com> <20180827231503.26899-11-adobriyan@gmail.com> <20180828123622.GA2087@darwi-kernel> <20180828130440.GA6464@darwi-kernel> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20180828130440.GA6464@darwi-kernel> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 28, 2018 at 01:04:40PM +0000, Ahmed S. Darwish wrote: > On Tue, Aug 28, 2018 at 12:36:22PM +0000, Ahmed S. Darwish wrote: > > On Tue, Aug 28, 2018 at 02:15:01AM +0300, Alexey Dobriyan wrote: > > > --- > > > fs/proc/base.c | 8 ++++---- > > > 1 file changed, 4 insertions(+), 4 deletions(-) > > > > > > > Missing description and S-o-b. Further comments below.. > > > > > diff --git a/fs/proc/base.c b/fs/proc/base.c > > > index 33f444721965..668e465c86b3 100644 > > > --- a/fs/proc/base.c > > > +++ b/fs/proc/base.c > > > @@ -3549,11 +3549,11 @@ static int proc_task_readdir(struct file *file, struct dir_context *ctx) > > > for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); > > > task; > > > task = next_tid(task), ctx->pos++) { > > > - char name[10 + 1]; > > > - unsigned int len; > > > + char name[10], *p = name + sizeof(name); > > > + > > > > Multiple issues: > > > > - len should be 11, as was in the original code > > (0xffffffff = 4294967295, 10 letters) > > > > - while we're at it, let's use a constant for the '11' instead of > > mysterious magic numbers > > > > - 'p' is clearly overflowing the stack here > > > > See below: > > > > tid = task_pid_nr_ns(task, ns); > > > - len = snprintf(name, sizeof(name), "%u", tid); > > > - if (!proc_fill_cache(file, ctx, name, len, > > > + p = _print_integer_u32(p, tid); > > > + if (!proc_fill_cache(file, ctx, p, name + sizeof(name) - p, > > > > You're replacing snprintf() code __that did proper len checking__ > > with code that does not. That's not good. > > > > I can't see how the fourth proc_fill_cache() parameter, ``name + > > sizeof(name)'' safely ever replace the original 'len' parameter. > > It's a pointer value .. (!) > > > > Ok, there's a "- p" in the end, so the length looks to be Ok. > > Nonetheless, the whole patch series is introducing funny code > like: > > +/* > + * Print an integer in decimal. > + * "p" initially points PAST THE END OF THE BUFFER! > + * > + * DO NOT USE THESE FUNCTIONS! > + * > + * Do not copy these functions. > + * Do not document these functions. > + * Do not move these functions to lib/ or elsewhere. > + * Do not export these functions to modules. > + * Do not tell anyone about these functions. > + */ > +noinline > +char *_print_integer_u32(char *p, u32 x) > +{ > + do { > + *--p = '0' + (x % 10); > + x /= 10; > + } while (x != 0); > + return p; > +} > > And thus the code using these functions is throwing invalid > past-the-stack pointers and strings with no NULL terminators > like there's no tomorrow... > > IMHO It's an accident waiting to happen to sprinkle pointers > like that everywhere. It is not if people will be prohibited from moving this code to lib/ and "improving" it by adding more parameters. > Are we really in a super hot path to justify all that? /proc is very slow, try profiling just about anything involving /proc.