Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2264148imm; Thu, 7 Jun 2018 07:55:29 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ7SuYIJbwR/Fjcp4HHNAhBwHZruwuCCQcFwBETv9KJiQ5xo7V59higepQXm0mewcV21CpZ X-Received: by 2002:a63:65c2:: with SMTP id z185-v6mr1856054pgb.163.1528383329920; Thu, 07 Jun 2018 07:55:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528383329; cv=none; d=google.com; s=arc-20160816; b=NvrI9KOfAsEdAqdT9JMpvlJUwHrfrKoNCztKkvmoMIirg9wz+zWnEI4RkCwKuO2wNA 6gUNvkzwfA4G2xRoLGugks3F1EKJkYC+cNULmnOPkfXeP+YLRVIWAvvyJgw7RYzRx7w3 ss4l/Y2v2RRcB0xImqE9Mn56H4O66LF4ou/xgX1ZyN9EPL5TB0LozJnbHdtfwcgpXzLD GjJk0rA9HbXRgL+8REtWpIF13gS7TGvV9u9MQbdbXVBqz2DX+mL6yL854w7V/6xs7jzl 7vLcBoqUB5yV+vQVkRsG+Y90yfAbibHa58t9vTvwpI4Jw6BhbEnFsNUFvVhkqpMf07Xr 326Q== 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:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=/7RjhCtfkXcf1/BcoqfOd7bAq65Q/KBhrUkgIUEbIQA=; b=Odso8FUN79DlkQF3PnRa9Z7lTu9tLc9MaqxjvrBpUX2r64QFH9sWA+XccwazEXMksx V1GmxpytHJDxfhv6IMjm2hj9UalfZbFHZ8PDebDhWXN5G7cD6XrOrdp30+aGBNJZ+Cbj fY6kDg9ZGOognjQATGjIaTxOGx4m7Uu5yFpzRoKF0mocfGtFLy0WlOaAdKvW2Bni8ivu O9Xv4T37fsfaqlh6r7Lfnl8KWtInDkPoAgKwEVOsO+TVdgqEBMV7+gCoAJoOUQP74ngS ZvC0Eu9DOzwn40dDR5MYeOIiADXSyZMvu/68UkmkjRKTnWODbIydfO/GEIbhFNmLjA7C 2abw== 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 5-v6si54796753plx.517.2018.06.07.07.55.15; Thu, 07 Jun 2018 07:55:29 -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; 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 S935372AbeFGOyY (ORCPT + 99 others); Thu, 7 Jun 2018 10:54:24 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:40989 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935200AbeFGOyU (ORCPT ); Thu, 7 Jun 2018 10:54:20 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbh-0005hJ-75; Thu, 07 Jun 2018 15:09:41 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb5-0002zO-Rj; Thu, 07 Jun 2018 15:09:03 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Linus Torvalds" , "Vegard Nossum" , "Tetsuo Handa" , "Michael Kerrisk (man-pages)" , "" , "Al Viro" , "Willy Tarreau" , "Jens Axboe" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 199/410] pipe: relocate round_pipe_size() above pipe_set_size() In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: "Michael Kerrisk (man-pages)" commit f491bd71118beba608d39ac2d5f1530e1160cd2e upstream. Patch series "pipe: fix limit handling", v2. When changing a pipe's capacity with fcntl(F_SETPIPE_SZ), various limits defined by /proc/sys/fs/pipe-* files are checked to see if unprivileged users are exceeding limits on memory consumption. While documenting and testing the operation of these limits I noticed that, as currently implemented, these checks have a number of problems: (1) When increasing the pipe capacity, the checks against the limits in /proc/sys/fs/pipe-user-pages-{soft,hard} are made against existing consumption, and exclude the memory required for the increased pipe capacity. The new increase in pipe capacity can then push the total memory used by the user for pipes (possibly far) over a limit. This can also trigger the problem described next. (2) The limit checks are performed even when the new pipe capacity is less than the existing pipe capacity. This can lead to problems if a user sets a large pipe capacity, and then the limits are lowered, with the result that the user will no longer be able to decrease the pipe capacity. (3) As currently implemented, accounting and checking against the limits is done as follows: (a) Test whether the user has exceeded the limit. (b) Make new pipe buffer allocation. (c) Account new allocation against the limits. This is racey. Multiple processes may pass point (a) simultaneously, and then allocate pipe buffers that are accounted for only in step (c). The race means that the user's pipe buffer allocation could be pushed over the limit (by an arbitrary amount, depending on how unlucky we were in the race). [Thanks to Vegard Nossum for spotting this point, which I had missed.] This patch series addresses these three problems. This patch (of 8): This is a minor preparatory patch. After subsequent patches, round_pipe_size() will be called from pipe_set_size(), so place round_pipe_size() above pipe_set_size(). Link: http://lkml.kernel.org/r/91a91fdb-a959-ba7f-b551-b62477cc98a1@gmail.com Signed-off-by: Michael Kerrisk Reviewed-by: Vegard Nossum Cc: Willy Tarreau Cc: Cc: Tetsuo Handa Cc: Jens Axboe Cc: Al Viro Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Ben Hutchings --- fs/pipe.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) --- a/fs/pipe.c +++ b/fs/pipe.c @@ -994,6 +994,18 @@ const struct file_operations pipefifo_fo }; /* + * Currently we rely on the pipe array holding a power-of-2 number + * of pages. + */ +static inline unsigned int round_pipe_size(unsigned int size) +{ + unsigned long nr_pages; + + nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; + return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; +} + +/* * Allocate a new array of pipe buffers and copy the info over. Returns the * pipe size if successful, or return -ERROR on error. */ @@ -1044,18 +1056,6 @@ static long pipe_set_size(struct pipe_in } /* - * Currently we rely on the pipe array holding a power-of-2 number - * of pages. - */ -static inline unsigned int round_pipe_size(unsigned int size) -{ - unsigned long nr_pages; - - nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; - return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; -} - -/* * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax * will return an error. */