Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4292197rwb; Tue, 20 Sep 2022 11:32:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Iu6GI9iekcPpW5I2eH166GyTp2/cVWDnfZQXXhvtNqA/xoVKQhULTK5rr0YtnrRs63RJL X-Received: by 2002:a05:6a02:20d:b0:430:3886:59e8 with SMTP id bh13-20020a056a02020d00b00430388659e8mr21242859pgb.516.1663698777342; Tue, 20 Sep 2022 11:32:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663698777; cv=none; d=google.com; s=arc-20160816; b=thm0UFXiZsNSr26SSlAUyGtwpQqeFgslhSWuugaKij40Ppwv468IQbnrJt79NdyhTK P0He4eScvBadfHwELqsJlKbFSBRYPdquSKHuieg5PhLyiYlBkxe9pBSB6yqpFqh9IWS1 DM945oxYlya637m/yNnILitB3MbPga1MePhImhHLq2gzq2GsVc8tXtAg2hdUNqw4xwlH tR1U5fb9Hp45TX9EtiAjMaLmyN+vEtCqnonm4o/lhz/8bMnU+MOIPKfmYBN2gkniojoV 5j293YwiSuk5yRuDp9QeY4wsZyGTXKuDA4aHxNuOtmgPOVsJCtCctIloWyaEgIbi5AjH uhdA== 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=b6rfJ+0Y1YLKFke5ZATSEQjt/w06yutYSnCcHkQyyI4=; b=HU7FLyqo6I5IAjweIJm+gmjf0MBgtwMZ/Zq+udB7OjEclsaN0GJ66KwzKIsDK/7MpK 1x0YXneV+0zOEFQygDueUnb/5AJ8kuKdq+TFHIw7JZ+/wcTKSFRo2ROlT2gwjYIw476I xLGcZ4pHB4CwZih7eeuw5pNSCtCASzomcNq784YG3LzWxa5CaYx/GbhZ8sS6BULY2BfY y+MoLQOneup9E7xF9vZYMGbpNI4J5t0tq7T07f1QhDgeR7JjN7Whg+7Hex4qROAEiChU i1dGYOIlcBgJqcHhjAmPIk/gMjhpRMUJZf7mnsLGHZT0tNk/Puy23mKD9JScycQIHzWg knYg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c20-20020a170902c1d400b001727963f929si424374plc.130.2022.09.20.11.32.45; Tue, 20 Sep 2022 11:32:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230225AbiITSJ1 (ORCPT + 99 others); Tue, 20 Sep 2022 14:09:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230506AbiITSJZ (ORCPT ); Tue, 20 Sep 2022 14:09:25 -0400 Received: from us-smtp-delivery-44.mimecast.com (us-smtp-delivery-44.mimecast.com [207.211.30.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C601470E68 for ; Tue, 20 Sep 2022 11:09:22 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-315-DWruCP5jNLe8nJwirNOEOA-1; Tue, 20 Sep 2022 14:09:16 -0400 X-MC-Unique: DWruCP5jNLe8nJwirNOEOA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A8DD18001B8; Tue, 20 Sep 2022 18:09:14 +0000 (UTC) Received: from comp-core-i7-2640m-0182e6.redhat.com (unknown [10.40.208.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 384462166B26; Tue, 20 Sep 2022 18:09:13 +0000 (UTC) From: Alexey Gladkov To: LKML , Linux Containers Cc: Andrew Morton , Christian Brauner , "Eric W . Biederman" , Kees Cook , Manfred Spraul Subject: [PATCH v2 2/3] sysctl: Allow to change limits for posix messages queues Date: Tue, 20 Sep 2022 20:08:21 +0200 Message-Id: <207698b81c7e55b3d00d2476b081a8fee5d369f6.1663696560.git.legion@kernel.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org All parameters of posix messages queues (queues_max/msg_max/msgsize_max) end up being limited by RLIMIT_MSGQUEUE. The code in mqueue_get_inode is where that limiting happens. The RLIMIT_MSGQUEUE is bound to the user namespace and is counted hierarchically. We can allow root in the user namespace to modify the posix messages queues parameters. Signed-off-by: Alexey Gladkov --- ipc/mq_sysctl.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/ipc/mq_sysctl.c b/ipc/mq_sysctl.c index fbf6a8b93a26..5e573c657775 100644 --- a/ipc/mq_sysctl.c +++ b/ipc/mq_sysctl.c @@ -12,6 +12,7 @@ #include #include #include +#include static int msg_max_limit_min = MIN_MSGMAX; static int msg_max_limit_max = HARD_MSGMAX; @@ -76,8 +77,45 @@ static int set_is_seen(struct ctl_table_set *set) return ¤t->nsproxy->ipc_ns->mq_set == set; } +static void mq_set_ownership(struct ctl_table_header *head, + struct ctl_table *table, + kuid_t *uid, kgid_t *gid) +{ + struct ipc_namespace *ns = + container_of(head->set, struct ipc_namespace, mq_set); + + kuid_t ns_root_uid = make_kuid(ns->user_ns, 0); + kgid_t ns_root_gid = make_kgid(ns->user_ns, 0); + + *uid = uid_valid(ns_root_uid) ? ns_root_uid : GLOBAL_ROOT_UID; + *gid = gid_valid(ns_root_gid) ? ns_root_gid : GLOBAL_ROOT_GID; +} + +static int mq_permissions(struct ctl_table_header *head, struct ctl_table *table) +{ + struct ipc_namespace *ns = + container_of(head->set, struct ipc_namespace, mq_set); + int mode = table->mode; + kuid_t ns_root_uid; + kgid_t ns_root_gid; + + mq_set_ownership(head, table, &ns_root_uid, &ns_root_gid); + + if (uid_eq(current_euid(), ns_root_uid)) + mode >>= 6; + + if (in_egroup_p(ns_root_gid)) + mode >>= 3; + + mode &= 7; + + return (mode << 6) | (mode << 3) | mode; +} + static struct ctl_table_root set_root = { .lookup = set_lookup, + .permissions = mq_permissions, + .set_ownership = mq_set_ownership, }; bool setup_mq_sysctls(struct ipc_namespace *ns) -- 2.33.4