Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp905658pxf; Wed, 7 Apr 2021 14:41:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz93aKULvVLJ2UPo/l4A4d35th9PnbY6tgD+vAo8ioNUaFE/m7slfqiDCBu2ZLKI8csfLlo X-Received: by 2002:a02:cc1b:: with SMTP id n27mr5565761jap.106.1617831717665; Wed, 07 Apr 2021 14:41:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617831717; cv=none; d=google.com; s=arc-20160816; b=VwSPJqgdwFgf/S2wbdgP/6zQhe2Ao/RzRk7oty+UFS0/s6FqZDaPwn/vyb4Aw2NG/y fXXh4BbqNI53+b2WWbRM9PoXSuao2kZ4IKMrLNcUexeMat6zpif05n1w8QTObHTIzu+g JSz6TGysWU2aLyn4P9rXJCU7mRfTT6j7ItFIb9sECprr1w8KyqocyJ/vB0ofo7I8VoYE OcMrtNkC9yE29eTL2IQObWo9cyIIOehZuLmir+Iviu3drCPH4OaAG6N4yHQGfGOEG7Xo o38/lIfNsDkYcWrvuUMIJ9l+An/YHwhYwSuJDKm4KQm6nGmuENBxh170U4REHTTtFW14 nQAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6SRMNCTILnxSnY8F4zkj6zpgm1NXKlyOA4fX0KnqHHM=; b=QzK7foNYTvzOeWFy3x0EKOlLjtR7MroliazUnucrSCes7bM79QZD28Xq/1NUvIlYFt F0qkw4eNJLa4XqGoq1g+NLcFOFaWcUMxT3e9vs7h5Pw31KY5iCmk90ykb9KftyywtB5I sw+/vedNOZw6u8bjj/CRrYDIbGj1XbPp9x8HM8+zVl4jJqPOYsiCgKom2bVe7MXOMVIH J01Go13gjBTNWXlGK5HPFz9YZhtaLuKiQx72QXPQfIc8ffqYDgg/1hp2QckcHu1eGYfQ 7dcWdI9L5Miw/NV763n3vLe+eP7VjAAKs8s1j8pbe8jSX7MIC0ac0k0snzEnPqRa74aT AVnQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x14si3750026jat.96.2021.04.07.14.41.45; Wed, 07 Apr 2021 14:41:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348180AbhDGRJU (ORCPT + 99 others); Wed, 7 Apr 2021 13:09:20 -0400 Received: from raptor.unsafe.ru ([5.9.43.93]:46990 "EHLO raptor.unsafe.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235711AbhDGRIv (ORCPT ); Wed, 7 Apr 2021 13:08:51 -0400 Received: from comp-core-i7-2640m-0182e6.redhat.com (ip-94-113-225-162.net.upcbroadband.cz [94.113.225.162]) by raptor.unsafe.ru (Postfix) with ESMTPSA id AE99F417CD; Wed, 7 Apr 2021 17:08:37 +0000 (UTC) From: Alexey Gladkov To: LKML , Kernel Hardening , Linux Containers , linux-mm@kvack.org Cc: Alexey Gladkov , Andrew Morton , Christian Brauner , "Eric W . Biederman" , Jann Horn , Jens Axboe , Kees Cook , Linus Torvalds , Oleg Nesterov Subject: [PATCH v10 9/9] ucounts: Set ucount_max to the largest positive value the type can hold Date: Wed, 7 Apr 2021 19:08:14 +0200 Message-Id: X-Mailer: git-send-email 2.29.3 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.4 (raptor.unsafe.ru [5.9.43.93]); Wed, 07 Apr 2021 17:08:38 +0000 (UTC) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The ns->ucount_max[] is signed long which is less than the rlimit size. We have to protect ucount_max[] from overflow and only use the largest value that we can hold. On 32bit using "long" instead of "unsigned long" to hold the counts has the downside that RLIMIT_MSGQUEUE and RLIMIT_MEMLOCK are limited to 2GiB instead of 4GiB. I don't think anyone cares but it should be mentioned in case someone does. The RLIMIT_NPROC and RLIMIT_SIGPENDING used atomic_t so their maximum hasn't changed. Signed-off-by: Alexey Gladkov --- include/linux/user_namespace.h | 6 ++++++ kernel/fork.c | 8 ++++---- kernel/user_namespace.c | 8 ++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index 82851fba7278..1c778182f5d5 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -123,6 +123,12 @@ bool inc_rlimit_ucounts_and_test(struct ucounts *ucounts, enum ucount_type type, void dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v); bool is_ucounts_overlimit(struct ucounts *ucounts, enum ucount_type type, unsigned long max); +static inline void set_rlimit_ucount_max(struct user_namespace *ns, + enum ucount_type type, unsigned long max) +{ + ns->ucount_max[type] = max <= LONG_MAX ? max : LONG_MAX; +} + #ifdef CONFIG_USER_NS static inline struct user_namespace *get_user_ns(struct user_namespace *ns) diff --git a/kernel/fork.c b/kernel/fork.c index a3a5e317c3c0..2cd01c443196 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -822,10 +822,10 @@ void __init fork_init(void) for (i = 0; i < MAX_PER_NAMESPACE_UCOUNTS; i++) init_user_ns.ucount_max[i] = max_threads/2; - init_user_ns.ucount_max[UCOUNT_RLIMIT_NPROC] = task_rlimit(&init_task, RLIMIT_NPROC); - init_user_ns.ucount_max[UCOUNT_RLIMIT_MSGQUEUE] = task_rlimit(&init_task, RLIMIT_MSGQUEUE); - init_user_ns.ucount_max[UCOUNT_RLIMIT_SIGPENDING] = task_rlimit(&init_task, RLIMIT_SIGPENDING); - init_user_ns.ucount_max[UCOUNT_RLIMIT_MEMLOCK] = task_rlimit(&init_task, RLIMIT_MEMLOCK); + set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_NPROC, task_rlimit(&init_task, RLIMIT_NPROC)); + set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_MSGQUEUE, task_rlimit(&init_task, RLIMIT_MSGQUEUE)); + set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_SIGPENDING, task_rlimit(&init_task, RLIMIT_SIGPENDING)); + set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_MEMLOCK, task_rlimit(&init_task, RLIMIT_MEMLOCK)); #ifdef CONFIG_VMAP_STACK cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 5ef0d4b182ba..df7651935fd5 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -121,10 +121,10 @@ int create_user_ns(struct cred *new) for (i = 0; i < MAX_PER_NAMESPACE_UCOUNTS; i++) { ns->ucount_max[i] = INT_MAX; } - ns->ucount_max[UCOUNT_RLIMIT_NPROC] = rlimit(RLIMIT_NPROC); - ns->ucount_max[UCOUNT_RLIMIT_MSGQUEUE] = rlimit(RLIMIT_MSGQUEUE); - ns->ucount_max[UCOUNT_RLIMIT_SIGPENDING] = rlimit(RLIMIT_SIGPENDING); - ns->ucount_max[UCOUNT_RLIMIT_MEMLOCK] = rlimit(RLIMIT_MEMLOCK); + set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)); + set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_MSGQUEUE, rlimit(RLIMIT_MSGQUEUE)); + set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_SIGPENDING, rlimit(RLIMIT_SIGPENDING)); + set_rlimit_ucount_max(ns, UCOUNT_RLIMIT_MEMLOCK, rlimit(RLIMIT_MEMLOCK)); ns->ucounts = ucounts; /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */ -- 2.29.3