Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp900035pxb; Fri, 22 Apr 2022 13:48:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzV1QTO50UL2rrt8tgI/xj5U1rpajiAlXAiYNLhzQLtpaTk3NiRjbEGhTRCU9z3SNEyn8Jv X-Received: by 2002:a17:903:2283:b0:158:e7f4:7056 with SMTP id b3-20020a170903228300b00158e7f47056mr6315408plh.24.1650660531847; Fri, 22 Apr 2022 13:48:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650660531; cv=none; d=google.com; s=arc-20160816; b=s2zk4CDkNsTYc8bIdDSBGHMFEP/67B6R02Zto00gYHZ6hlShAcAlfzDBzpkgqeQst5 cdtKm+HFvbazg8jQcw+Psg/vMQODDOOQK5ecmEbFUjQPlVc5/Yeuf+4OZrwMUn4KjGge nApKob0CunIXKoSXBMLDmtlrCZsngmlU0Ifc85u8W72fL3wwax431vwf2vpWewQ5eiwv DHr14yPbqbQhgCFql12cokrxGowcpgv3/iotUQZ+VYjmNJKcckD72UN6mmC3cG+67lZU 7lXwt/teL4+EY8uek32zq/l/LNaE4/1aFnP2dB45hsX0es65UtsZ+v4vFdLqpKqtmyi/ GRwQ== 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=Y38N4h5birxjzml4tM8wxrGUeOV7hjyORiDZwVRy7gs=; b=MiwtuaHCxIhaVwgJ3SOZghmAEiOicSoKnFsO2s1HolfkwkxvswZ9O53YFwmoI6fuht vY9VtZcldFBE1kPvcz+ghBERiyCHHay0H0+sFihqUOuC+mZnx9zm5IPH5WIwvqa44O+x kaZOEtHV/KVS6OvuSn5OTh8eIwh9w8I6xiDmypXT4IpDGnoaFAQg26h1c3w9uNVQUiJn wTPDnbmTY8JPiJ6Wz1BQPkvACIgw5xAi95faCCdnBWRL3IKDWixt3eACHPG0+bxG1I8/ qZMFLv71nE0YSPX1Q8N/nFpnHptI0EkMF+usJsOsyVkZsVqLk5DJar1SVfpXQ4lXHFmC Be8A== 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:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k191-20020a633dc8000000b003aad34c72ebsi1703958pga.618.2022.04.22.13.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 13:48:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A44DD1F3527; Fri, 22 Apr 2022 12:38:45 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233919AbiDVM5z (ORCPT + 99 others); Fri, 22 Apr 2022 08:57:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233826AbiDVM4z (ORCPT ); Fri, 22 Apr 2022 08:56:55 -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 ESMTP id A1386527F5 for ; Fri, 22 Apr 2022 05:54:02 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-369-Yr3lvMn-Mpec8zFWnc3kCg-1; Fri, 22 Apr 2022 08:53:58 -0400 X-MC-Unique: Yr3lvMn-Mpec8zFWnc3kCg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 12A603C1E8FA; Fri, 22 Apr 2022 12:53:57 +0000 (UTC) Received: from comp-core-i7-2640m-0182e6.redhat.com (unknown [10.36.110.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57964C44CCF; Fri, 22 Apr 2022 12:53:54 +0000 (UTC) From: Alexey Gladkov To: LKML , "Eric W . Biederman" , Linus Torvalds Cc: Alexander Mikhalitsyn , Andrew Morton , Christian Brauner , Daniel Walsh , Davidlohr Bueso , Kirill Tkhai , Linux Containers , Manfred Spraul , Serge Hallyn , Varad Gautam , Vasily Averin Subject: [PATCH v1 3/4] ipc: Check permissions for checkpoint_restart sysctls at open time Date: Fri, 22 Apr 2022 14:53:39 +0200 Message-Id: <8b415b6b297d1143106bbcf57a269eb744bfdf81.1650631347.git.legion@kernel.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE 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 As Eric Biederman pointed out, it is possible not to use a custom proc_handler and check permissions for every write, but to use a .permission handler. That will allow the checkpoint_restart sysctls to perform all of their permission checks at open time, and not need any other special code. Link: https://lore.kernel.org/lkml/87czib9g38.fsf@email.froward.int.ebiederm.org/ Signed-off-by: Eric W. Biederman Signed-off-by: Alexey Gladkov --- ipc/ipc_sysctl.c | 54 ++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c index ff99d0305a5b..5a58598d48c8 100644 --- a/ipc/ipc_sysctl.c +++ b/ipc/ipc_sysctl.c @@ -68,25 +68,6 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write, return ret; } -#ifdef CONFIG_CHECKPOINT_RESTORE -static int proc_ipc_dointvec_minmax_checkpoint_restore(struct ctl_table *table, - int write, void *buffer, size_t *lenp, loff_t *ppos) -{ - struct ipc_namespace *ns = current->nsproxy->ipc_ns; - struct ctl_table ipc_table; - - if (write && !checkpoint_restore_ns_capable(ns->user_ns)) - return -EPERM; - - memcpy(&ipc_table, table, sizeof(ipc_table)); - - ipc_table.extra1 = SYSCTL_ZERO; - ipc_table.extra2 = SYSCTL_INT_MAX; - - return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos); -} -#endif - int ipc_mni = IPCMNI; int ipc_mni_shift = IPCMNI_SHIFT; int ipc_min_cycle = RADIX_TREE_MAP_SIZE; @@ -172,22 +153,28 @@ static struct ctl_table ipc_sysctls[] = { .procname = "sem_next_id", .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id, .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id), - .mode = 0666, - .proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore, + .mode = 0444, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_INT_MAX, }, { .procname = "msg_next_id", .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id, .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id), - .mode = 0666, - .proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore, + .mode = 0444, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_INT_MAX, }, { .procname = "shm_next_id", .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id, .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id), - .mode = 0666, - .proc_handler = proc_ipc_dointvec_minmax_checkpoint_restore, + .mode = 0444, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_INT_MAX, }, #endif {} @@ -203,8 +190,25 @@ static int set_is_seen(struct ctl_table_set *set) return ¤t->nsproxy->ipc_ns->ipc_set == set; } +static int ipc_permissions(struct ctl_table_header *head, struct ctl_table *table) +{ + int mode = table->mode; + +#ifdef CONFIG_CHECKPOINT_RESTORE + struct ipc_namespace *ns = current->nsproxy->ipc_ns; + + if (((table->data == &ns->ids[IPC_SEM_IDS].next_id) || + (table->data == &ns->ids[IPC_MSG_IDS].next_id) || + (table->data == &ns->ids[IPC_SHM_IDS].next_id)) && + checkpoint_restore_ns_capable(ns->user_ns)) + mode = 0666; +#endif + return mode; +} + static struct ctl_table_root set_root = { .lookup = set_lookup, + .permissions = ipc_permissions, }; bool setup_ipc_sysctls(struct ipc_namespace *ns) -- 2.33.2