From patchwork Thu Dec 14 12:50:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13493006 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBF1AC4167B for ; Thu, 14 Dec 2023 12:51:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E4EB8D00B0; Thu, 14 Dec 2023 07:51:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 246C18D00A2; Thu, 14 Dec 2023 07:51:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3F728D00B0; Thu, 14 Dec 2023 07:51:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DFBBB8D00A2 for ; Thu, 14 Dec 2023 07:51:48 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BD5F3A226F for ; Thu, 14 Dec 2023 12:51:48 +0000 (UTC) X-FDA: 81565410696.22.5520E01 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf24.hostedemail.com (Postfix) with ESMTP id EF99918000D for ; Thu, 14 Dec 2023 12:51:46 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QjrKy23M; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702558307; a=rsa-sha256; cv=none; b=TcP/7yo13+kaeaEAH4nqLiHIROZ4aTia/ywSouSrifGwaQ7XUjjsLgxTzO+ei1ot1oSO73 bKVkWTKmw2yqxzLjbmEMdVe5ua2CwhBn2Ojzp4ANRajSVMbf1V7Wk4r3AfAaFpFI+8GTA0 gnrT2yr/5iptx3bqgNt4aSFyE7oSRmc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QjrKy23M; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702558307; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F5jNCiht2qhfhgmL/0QXVPaECx4NcLPjGSo+ppuw/OE=; b=mH6HmINOtzS54eBfX3QHPCe4ddABCcqp/Fiwgc7YTR+YQYGnmz7gqRdMFFTjO+46+EClQ6 SuW5Vnpj//5jxVXLwNnE/Hhs803kK4r+NyrlD/bBcRwveRquJTU5l69sc5f9TQsCV6iMKY K4v6b6b7WbZgk8LhEddWtbG83f1BicU= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1d350dff621so16431575ad.1 for ; Thu, 14 Dec 2023 04:51:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702558306; x=1703163106; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F5jNCiht2qhfhgmL/0QXVPaECx4NcLPjGSo+ppuw/OE=; b=QjrKy23MCgBs1kmE3GvjUhSzBZmogR42mz0Oab5+y2wv9yQy/NV0tnMl3w11W+kSVE 0T/ZZtrCK0ucJFjYGZ5C1UcyWsmHfa/gUittEwzT7vgLzqIEMSg5q4UNmdwjDuzsRExc MVPcG2PwgC11yGNCaU8FyxulVoSxnkdh78BygkHqH3tmz6F8Cgzs4W4v4KQk3s3/uYqy 6Do5Ks0/TjbOd2kdGwxFUfBG4S1tNOI34MG+jnatkBh5/GXP/q70Cenc8vsbkJuQT38T OUQZq5BlvkE2J1s0OnZPt/Qj3wwH8kvzpuwwjEya6enPKQVJvwpCMTqdCtvZV+dfAUlG uokA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702558306; x=1703163106; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F5jNCiht2qhfhgmL/0QXVPaECx4NcLPjGSo+ppuw/OE=; b=rXYlJdC/qRhSETbMYJllN/etBTHcC4HwVgP9MCQSC07+T5+0PQDmysZUw8AbirWci2 OncX2ch6gtRxOaY2EcfSnjRKG8gup97XW/9x3JVsV4zsbf+XNSERqsKlSpa8Q5Q7xjn0 A8Wi99nJYlVGn1nGhzA69Qq5Wnax0BpjQsNBJ+/D/ThR/9I+WyO6YYo1Dj9WsNbuJwsy aCibqF2sOM0+IEuRhY5J3hCp0ku6Yu5gC4mnlo8TUHdxmJj6ezC4Zqh4+kACf/WO9eU5 toxagtWzL0KQBrErHucMMFKUQ61M3OetypPHFRtIfNzJuYSI274P3SjIRpxkvt/rOVF1 Qtvg== X-Gm-Message-State: AOJu0YxAfpxEmPN2MB5TRQhp3TxkK8H47Mzh7KO3QQNIdVS2MGC0KHGx 2jP5UTlhA/d+uyyv2oh6+vE= X-Google-Smtp-Source: AGHT+IGpqBwIV6/Z1bJz2uXsgixmwbGfGPBUTyaTQL8gEPBIKToLlEPZWt43poMVRrkjwahDoJ6KPg== X-Received: by 2002:a17:903:22c1:b0:1d0:6ffd:9e2a with SMTP id y1-20020a17090322c100b001d06ffd9e2amr10329616plg.124.1702558305872; Thu, 14 Dec 2023 04:51:45 -0800 (PST) Received: from vultr.guest ([149.28.194.201]) by smtp.gmail.com with ESMTPSA id jj17-20020a170903049100b001d36b2e3dddsm1184528plb.192.2023.12.14.04.51.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 04:51:45 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, casey@schaufler-ca.com, kpsingh@kernel.org, mhocko@suse.com, ying.huang@intel.com Cc: linux-mm@kvack.org, linux-security-module@vger.kernel.org, bpf@vger.kernel.org, ligang.bdlg@bytedance.com, Yafang Shao Subject: [PATCH v5 bpf-next 3/5] mm, security: Add lsm hook for memory policy adjustment Date: Thu, 14 Dec 2023 12:50:31 +0000 Message-Id: <20231214125033.4158-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231214125033.4158-1-laoar.shao@gmail.com> References: <20231214125033.4158-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: EF99918000D X-Stat-Signature: cgs1ueqdcnubf6xs7tft1g1a3qrswmer X-HE-Tag: 1702558306-844448 X-HE-Meta: U2FsdGVkX19MaQD+O2i2X1+0j7tidS/LTqNjf0hyd/KO1KwvCxHQ7De2q8/Vy/UMb/GmMm1lHuZekbqP7IAHW+9YYALQnVLrjcoMTDD8ePiUV6ww2Gc1anf3lVUcMKfgCujPLN1XR8d0+5IurGF1PMADhi91QQmKMJhCS58fdN3r0H+uN4vnWG92q8ilDloRSdKVAlpiqE4Thza9/vn3ZZGRGy6RXgIJ1TFvJXc1RYE6hlL1YFt4F7vOLG4nQZphZF8C8De4zMyHUC76Bm/PEvMWCk4rVrDzmOtM9i4R3pzbvgTCGjn87dmTTaCIfgKb9BjRi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QYbVYwQA1xS0oxM7RwBePVSdz3o2M4gKz+Tuv7gAtKZ0alik+iD5x92OYFaqH5pAphsI0c3/zuRkldZKwaADiAwE087AsaJ6f6aZDd6FNEU0tCoA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In a containerized environment, independent memory binding by a user can lead to unexpected system issues or disrupt tasks being run by other users on the same server. If a user genuinely requires memory binding, we will allocate dedicated servers to them by leveraging kubelet deployment. At present, users have the capability to bind their memory to a specific node without explicit agreement or authorization from us. Consequently, a new LSM hook is introduced to mitigate this. This implementation allows us to exercise fine-grained control over memory policy adjustments within our container environment Signed-off-by: Yafang Shao --- include/linux/lsm_hook_defs.h | 3 +++ include/linux/security.h | 9 +++++++++ mm/mempolicy.c | 8 ++++++++ security/security.c | 13 +++++++++++++ 4 files changed, 33 insertions(+) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index ff217a5..5580127 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -419,3 +419,6 @@ LSM_HOOK(int, 0, uring_sqpoll, void) LSM_HOOK(int, 0, uring_cmd, struct io_uring_cmd *ioucmd) #endif /* CONFIG_IO_URING */ + +LSM_HOOK(int, 0, set_mempolicy, unsigned long mode, unsigned short mode_flags, + nodemask_t *nmask, unsigned int flags) diff --git a/include/linux/security.h b/include/linux/security.h index 1d1df326..cc4a19a 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -484,6 +484,8 @@ int security_setprocattr(const char *lsm, const char *name, void *value, int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen); int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen); int security_locked_down(enum lockdown_reason what); +int security_set_mempolicy(unsigned long mode, unsigned short mode_flags, + nodemask_t *nmask, unsigned int flags); #else /* CONFIG_SECURITY */ static inline int call_blocking_lsm_notifier(enum lsm_event event, void *data) @@ -1395,6 +1397,13 @@ static inline int security_locked_down(enum lockdown_reason what) { return 0; } + +static inline int +security_set_mempolicy(unsigned long mode, unsigned short mode_flags, + nodemask_t *nmask, unsigned int flags) +{ + return 0; +} #endif /* CONFIG_SECURITY */ #if defined(CONFIG_SECURITY) && defined(CONFIG_WATCH_QUEUE) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 10a590e..9535d9e 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1483,6 +1483,10 @@ static long kernel_mbind(unsigned long start, unsigned long len, if (err) return err; + err = security_set_mempolicy(lmode, mode_flags, &nodes, flags); + if (err) + return err; + return do_mbind(start, len, lmode, mode_flags, &nodes, flags); } @@ -1577,6 +1581,10 @@ static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask, if (err) return err; + err = security_set_mempolicy(lmode, mode_flags, &nodes, 0); + if (err) + return err; + return do_set_mempolicy(lmode, mode_flags, &nodes); } diff --git a/security/security.c b/security/security.c index dcb3e70..685ad79 100644 --- a/security/security.c +++ b/security/security.c @@ -5337,3 +5337,16 @@ int security_uring_cmd(struct io_uring_cmd *ioucmd) return call_int_hook(uring_cmd, 0, ioucmd); } #endif /* CONFIG_IO_URING */ + +/** + * security_set_mempolicy() - Check if memory policy can be adjusted + * @mode: The memory policy mode to be set + * @mode_flags: optional mode flags + * @nmask: modemask to which the mode applies + * @flags: mode flags for mbind(2) only + */ +int security_set_mempolicy(unsigned long mode, unsigned short mode_flags, + nodemask_t *nmask, unsigned int flags) +{ + return call_int_hook(set_mempolicy, 0, mode, mode_flags, nmask, flags); +}