From patchwork Fri Dec 8 09:06:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13485031 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="E/DRJ7vk" Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A82A10F1; Fri, 8 Dec 2023 01:06:43 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1d0aaa979f0so14208615ad.0; Fri, 08 Dec 2023 01:06:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702026403; x=1702631203; darn=vger.kernel.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=5FXGf9L1hz/wQQ/jUv0sNTidQzM378s4gfN4y5IQE/M=; b=E/DRJ7vkip0DXH3MTqZgLBRkgyu0Qx4wHpAM+PElVOLu1lPYAXxtQsc7u5FRMFItFT qXWTj3+uPOkQ3CaNMT1JX4meGzRSWBjyKXOKU05iW5PhKCuU+C6SWPzukc2+03pDTFiz lnZJeQmD389wGMJrBh1ZyVRVfdBV6G6k4sN1X4gimkgOcGpHcz6W68IL8bRyt5ysquJr GAXVPzPYmJpOsQ53nSfRs4FCPBjoidKCyq31HwkrtFeKO6JEZYarDDwaGhb21nI1tb4c TDGP4cpEWNbMYxRMFsRnByE2g4Pg20kPF2JrQzyWZawWOyFJ/Ga49TdKh+4ilHUI9VBO nwDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702026403; x=1702631203; 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=5FXGf9L1hz/wQQ/jUv0sNTidQzM378s4gfN4y5IQE/M=; b=PHgZsZhx2ABYpSR+VTs++mDI3EfSs/50+F2zqTy3ubm9pJ2G42/piebhLUhRhdlmxP A5hPZ/YKE17+ZJ+wEImZKpUHtGGbtm45110xFgjEB8qMnHUXM5Vfi6QOHCnkxuJZyTUm K38jayU+7oc0nIKHFkI7TtfyawfqNiE6Z7WYQ+vGR8PCzG4vMLMxXyWc3bfVEXaVTLSP 5o6z9NADIBXYZRbGQmDhWV/R8XQcTq+KPcrFdTtmTHbvNzGq/hpzfFX5HdEPl8OhAVFh rZl+5xK0EjxmRyIeXt/8wkE/AeYWJFJEN3jB+aClL3VokpGk7eXJ59UXN9YPcj5NCKEE 8j2Q== X-Gm-Message-State: AOJu0YySqD2QWS2YuYY+ZQC60LvjN0q0kGL/2xN6pvGafCXe82XVaJVA n4cz5zwUPQMrKTvulakp0Ni0CciJSFNnb43S1D0= X-Google-Smtp-Source: AGHT+IFlw/JLS1hN3T0ahEP+Fd1/GMCS9LQMI06Z1/BK/WYt8tyi7FPO5YKhxacr/lJxGfNAdtearg== X-Received: by 2002:a17:903:22cb:b0:1d0:a7b7:74b7 with SMTP id y11-20020a17090322cb00b001d0a7b774b7mr3612483plg.104.1702026402988; Fri, 08 Dec 2023 01:06:42 -0800 (PST) Received: from vultr.guest ([2001:19f0:ac00:4055:5400:4ff:fead:3bd0]) by smtp.gmail.com with ESMTPSA id 21-20020a170902ee5500b001d057080022sm1188173plo.20.2023.12.08.01.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 01:06:42 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, 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 v4 1/5] mm, doc: Add doc for MPOL_F_NUMA_BALANCING Date: Fri, 8 Dec 2023 09:06:18 +0000 Message-Id: <20231208090622.4309-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231208090622.4309-1-laoar.shao@gmail.com> References: <20231208090622.4309-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The document on MPOL_F_NUMA_BALANCING was missed in the initial commit The MPOL_F_NUMA_BALANCING document was inadvertently omitted from the initial commit bda420b98505 ("numa balancing: migrate on fault among multiple bound nodes") Let's ensure its inclusion. Signed-off-by: Yafang Shao Reviewed-by: "Huang, Ying" --- .../admin-guide/mm/numa_memory_policy.rst | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Documentation/admin-guide/mm/numa_memory_policy.rst b/Documentation/admin-guide/mm/numa_memory_policy.rst index eca38fa..19071b71 100644 --- a/Documentation/admin-guide/mm/numa_memory_policy.rst +++ b/Documentation/admin-guide/mm/numa_memory_policy.rst @@ -332,6 +332,33 @@ MPOL_F_RELATIVE_NODES MPOL_PREFERRED policies that were created with an empty nodemask (local allocation). +MPOL_F_NUMA_BALANCING (since Linux 5.12) + When operating in MPOL_BIND mode, enables NUMA balancing for tasks, + contingent upon kernel support. This feature optimizes page + placement within the confines of the specified memory binding + policy. The addition of the MPOL_F_NUMA_BALANCING flag augments the + control mechanism for NUMA balancing: + + - The sysctl knob numa_balancing governs global activation or + deactivation of NUMA balancing. + + - Even if sysctl numa_balancing is enabled, NUMA balancing remains + disabled by default for memory areas or applications utilizing + explicit memory policies. + + - The MPOL_F_NUMA_BALANCING flag facilitates NUMA balancing + activation for applications employing explicit memory policies + (MPOL_BIND). + + This flags enables various optimizations for page placement through + NUMA balancing. For instance, when an application's memory is bound + to multiple nodes (MPOL_BIND), the hint page fault handler attempts + to migrate accessed pages to reduce cross-node access if the + accessing node aligns with the policy nodemask. + + If the flag isn't supported by the kernel, or is used with mode + other than MPOL_BIND, -1 is returned and errno is set to EINVAL. + Memory Policy Reference Counting ================================ From patchwork Fri Dec 8 09:06:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13485032 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XH6DXY8D" Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C66A170F; Fri, 8 Dec 2023 01:06:45 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d04c097e34so15036265ad.0; Fri, 08 Dec 2023 01:06:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702026404; x=1702631204; darn=vger.kernel.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=xHJ+KYyJ1jZcwPU6L/9C8DimrQ09bt4oL7onSyu8iGg=; b=XH6DXY8D5gyFTslQppy+82NmXrapItp3XX5qEm+B80K8SC85UTAO/PJe/uanOA1rIB MhfxlsptXdYNOZdTC2yG6pcgrSjGahv69Uh7kU0yuskfl4VgI6OLO1MC5rfSh1ON/BMo WavjjOSgL0hV1QkN4QYRHibAkfrZgejcKavmta98kA5mJSZKupbKFa4eMGsgocWqn3B8 xk67wpW+xgJoAug5ZNSZe8RkDeNpjradl2WWdvHpZnlwmv5wAU1N3+OFNiAA6Pvvt96e +NrZtvbugPEiRXxMk3mx2X480k2sOs35v6DU/vPcXk0vZTMJplcKgZB/AEtnuROw0U3L 8fwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702026404; x=1702631204; 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=xHJ+KYyJ1jZcwPU6L/9C8DimrQ09bt4oL7onSyu8iGg=; b=LwCHn5OqdtKBnI/z+DN0GTz6SFr6Ry0tg5Q/AuaYIyfljcL2scV3YjjgPMk7pSK+U/ HNtcIpnRNRucG180mdA9YLN8YiRP62Q9N0y9ICEs/mkLAzcIyCSY3PLLmT37bGWZOVX3 VWIB+nwDIfqjez0Xd6/OJm77e6xE+ItfXDR6eEPoqbGgZeEO1H5UpYBt2F51upGebXB5 E2cOvl69zuU727b1cJExPzRTUuC3jgGZ8f+j6M1Kx0ZEp/8kEAfZo1S1T6dssvI6F9wD ptIB1/cAJRglkScf5n7t4Wo61pdlvKAtXzw1p6AE5be/W1dXib3wb+rBujrllMWBJoxh yOZw== X-Gm-Message-State: AOJu0Yy4soRU9INE1uLFmtW2cLhQVFQiSl7VIE5MmPJEixpU55LGKHbB skrKFEIuJv+coW1DlH9woVQ= X-Google-Smtp-Source: AGHT+IGDD+5m4P7ro9+jd2WHzgWnxmWzxe8aGNd9wryRLo3x0jXWLZRXgUnqD7rwbEtBQ9pvrJvDCw== X-Received: by 2002:a17:902:d54c:b0:1d0:6ffd:6101 with SMTP id z12-20020a170902d54c00b001d06ffd6101mr856123plf.35.1702026404515; Fri, 08 Dec 2023 01:06:44 -0800 (PST) Received: from vultr.guest ([2001:19f0:ac00:4055:5400:4ff:fead:3bd0]) by smtp.gmail.com with ESMTPSA id 21-20020a170902ee5500b001d057080022sm1188173plo.20.2023.12.08.01.06.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 01:06:44 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, 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 , Eric Dumazet Subject: [PATCH v4 2/5] mm: mempolicy: Revise comment regarding mempolicy mode flags Date: Fri, 8 Dec 2023 09:06:19 +0000 Message-Id: <20231208090622.4309-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231208090622.4309-1-laoar.shao@gmail.com> References: <20231208090622.4309-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 MPOL_F_STATIC_NODES, MPOL_F_RELATIVE_NODES, and MPOL_F_NUMA_BALANCING are mode flags applicable to both set_mempolicy(2) and mbind(2) system calls. It's worth noting that MPOL_F_NUMA_BALANCING was initially introduced in commit bda420b98505 ("numa balancing: migrate on fault among multiple bound nodes") exclusively for set_mempolicy(2). However, it was later made a shared flag for both set_mempolicy(2) and mbind(2) following commit 6d2aec9e123b ("mm/mempolicy: do not allow illegal MPOL_F_NUMA_BALANCING | MPOL_LOCAL in mbind()"). This revised version aims to clarify the details regarding the mode flags. Signed-off-by: Yafang Shao Reviewed-by: "Huang, Ying" Cc: Eric Dumazet --- include/uapi/linux/mempolicy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/uapi/linux/mempolicy.h b/include/uapi/linux/mempolicy.h index a8963f7..afed4a4 100644 --- a/include/uapi/linux/mempolicy.h +++ b/include/uapi/linux/mempolicy.h @@ -26,7 +26,7 @@ enum { MPOL_MAX, /* always last member of enum */ }; -/* Flags for set_mempolicy */ +/* Flags for set_mempolicy() or mbind() */ #define MPOL_F_STATIC_NODES (1 << 15) #define MPOL_F_RELATIVE_NODES (1 << 14) #define MPOL_F_NUMA_BALANCING (1 << 13) /* Optimize with NUMA balancing if possible */ From patchwork Fri Dec 8 09:06:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13485033 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bqXOjMZs" Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87F4B10FC; Fri, 8 Dec 2023 01:06:46 -0800 (PST) Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-5c6ce4dffb5so1335786a12.0; Fri, 08 Dec 2023 01:06:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702026406; x=1702631206; darn=vger.kernel.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=bqXOjMZstiNzXCeuJFFnutI/xdHB1Mg9BPrEbSV5jPejkqMY4mw3foLuufpawJ00rU AqloSsi8756OhYOuNUefYfULitd/U9IYeshWi3q/X8Z2Q9nFyJoOMvg+k0eVgP46WhjG uS6QJH1JK3AUuGF/G6IQidPMWsrtYvBGQgTU9hC9JlyD0O4YyWBCZp0x7vKVAMrYw8uu XIMyRX2ye2KVknw7wz60h+xAU+LX99FEPHEfJmIKLFeMA3++Pe80O4RZ1rq7gcb2D+0U KxEJJ1FI2mmmb2XjOq/Xyd6Hu0lbHHjzX5Hb0M2IMeiaNE4b1ZiYz3DvEOjjUYwcOZwD g+Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702026406; x=1702631206; 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=D1L1KKWzC4637CaCl3uGcnsF7/Hf403zaH6I3TGn+YepQSh0nYPq8n9lwASbt5aYn5 M74d5o37tSHHqqVXmYw0oDI53Rp2tK/WL5f724g+4Lqc5J7owqE1FsrVzmlmaIc4gT2M 2N14/vc9ewj9xB/G/JSPZC9nLK+Nwww6puunYBmk7U0f7fx5VilKIMMuz0sDLLrpHrLJ h1niuNDEEzhEoUqXlK+25J15wb4tSGoGWXLiFCXuhKaSqIYKyDAlXNpTo15lwqRM7yN1 IXaVsflFIeI88EPdnudRsTgSnypw9i9mv5imrpLo8QgYS0oADius9jd7sbJ5vTRx3akQ rUNw== X-Gm-Message-State: AOJu0YxaZsDGL3kWMtHN8b14k9hHXjyHP1W/9V9EgubypH6pxtuKJ4QE fhfex7w6hMqdVL1ROx/fqSk= X-Google-Smtp-Source: AGHT+IEJsoOkgNRvqCeC96DE0QTf43JoGq3IIKgG7QBj8teRqPSX55EqjkbrCFFIwKXuc0Teb88VDg== X-Received: by 2002:a17:90b:38ca:b0:286:f19f:5022 with SMTP id nn10-20020a17090b38ca00b00286f19f5022mr3458016pjb.27.1702026406051; Fri, 08 Dec 2023 01:06:46 -0800 (PST) Received: from vultr.guest ([2001:19f0:ac00:4055:5400:4ff:fead:3bd0]) by smtp.gmail.com with ESMTPSA id 21-20020a170902ee5500b001d057080022sm1188173plo.20.2023.12.08.01.06.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 01:06:45 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, 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 v4 3/5] mm, security: Add lsm hook for memory policy adjustment Date: Fri, 8 Dec 2023 09:06:20 +0000 Message-Id: <20231208090622.4309-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231208090622.4309-1-laoar.shao@gmail.com> References: <20231208090622.4309-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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); +} From patchwork Fri Dec 8 09:06:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13485034 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KtlA6IcU" Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F18C2171C; Fri, 8 Dec 2023 01:06:47 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d1e1edb10bso16931185ad.1; Fri, 08 Dec 2023 01:06:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702026407; x=1702631207; darn=vger.kernel.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=6NLQupAZumzbZoKpNAG12sWAtAVUuthZdQhUM0QKlyo=; b=KtlA6IcUmzvgdy5ncakz9CcbDOjJ4sdpwTW4jleIsGbMjCZc+UMiClM4e8ffvQF5Dy imItZQg9jpMDglEaNc2n/Gni4OfqpcoYG3F/uDw7ODfgo63GCtURiD1Qgg3s5OERDa89 BRPnjdkBw/wwrnbsYrbr2XAXetO32mPLrJcV7n8lukiO2ivWjaU31+++P4RaxhsxDem4 /YAMreuvVEyIE882fegqaewhMftkillwtqdDNZaUnL5L1GAGlI5MXbaOcgwn3Ko0bQSz pmWFsOK+8qMK0RIrVYIeyN9TlUbDsjMQ0491ytPKHLRpQPZdiSMnsFs/m4nOXSt1azY7 bQfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702026407; x=1702631207; 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=6NLQupAZumzbZoKpNAG12sWAtAVUuthZdQhUM0QKlyo=; b=akWobL6NxqWVBSH1aM+DeIin8bJbx6FYQw8Jfu+o6iLITjGd4VMInSUQqQJ4lnLuo8 hKMON58Jx2b0rrWu8GyGysTxItt1khv7IcectLnqXDGZRXDFZtb3S3RUuLRSX3zlKfZt 9LFxGX2PjWXjkSH4LctFOK6lOHi/F7oZ2GmywhzsHuP9Df+nhH2rMbSsQNGTDi8dbTxS td6vUkfkcM8UyvBJfHJn01r5CuziDdc3g/mBgEbytI5tSouMkkpuinwegDUwuxPgsjbD 0MVCPEwH1OPf1RRKF4S23w0B5Nu8dcKGgp09654fqGZqkL+XpGxQ03Brcu9YFjXk0vgP npHQ== X-Gm-Message-State: AOJu0YxHcb4+ER2XD/VaN4d1FTytaocTAX1HK+mYa8glEV7nItg7yehj KNqX20Ldoq1hGtA8doqQUDo= X-Google-Smtp-Source: AGHT+IFZVEkMU90veuH9AP4AV43iIfPMyna/tHibWLIRr/K+v5B3aBILYxNOBzTd1wYRXklDq0Kt2w== X-Received: by 2002:a17:903:41ce:b0:1d0:7c31:197e with SMTP id u14-20020a17090341ce00b001d07c31197emr4774284ple.57.1702026407392; Fri, 08 Dec 2023 01:06:47 -0800 (PST) Received: from vultr.guest ([2001:19f0:ac00:4055:5400:4ff:fead:3bd0]) by smtp.gmail.com with ESMTPSA id 21-20020a170902ee5500b001d057080022sm1188173plo.20.2023.12.08.01.06.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 01:06:46 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, 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 v4 4/5] security: selinux: Implement set_mempolicy hook Date: Fri, 8 Dec 2023 09:06:21 +0000 Message-Id: <20231208090622.4309-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231208090622.4309-1-laoar.shao@gmail.com> References: <20231208090622.4309-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a SELinux access control for the newly introduced set_mempolicy lsm hook. A new permission "setmempolicy" is defined under the "process" class for it. Signed-off-by: Yafang Shao --- security/selinux/hooks.c | 8 ++++++++ security/selinux/include/classmap.h | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index feda711..1528d4d 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -4238,6 +4238,13 @@ static int selinux_userns_create(const struct cred *cred) USER_NAMESPACE__CREATE, NULL); } +static int selinux_set_mempolicy(unsigned long mode, unsigned short mode_flags, + nodemask_t *nmask, unsigned int flags) +{ + return avc_has_perm(current_sid(), task_sid_obj(current), SECCLASS_PROCESS, + PROCESS__SETMEMPOLICY, NULL); +} + /* Returns error only if unable to parse addresses */ static int selinux_parse_skb_ipv4(struct sk_buff *skb, struct common_audit_data *ad, u8 *proto) @@ -7072,6 +7079,7 @@ static int selinux_uring_cmd(struct io_uring_cmd *ioucmd) LSM_HOOK_INIT(task_kill, selinux_task_kill), LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), LSM_HOOK_INIT(userns_create, selinux_userns_create), + LSM_HOOK_INIT(set_mempolicy, selinux_set_mempolicy), LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h index a3c3807..c280d92 100644 --- a/security/selinux/include/classmap.h +++ b/security/selinux/include/classmap.h @@ -51,7 +51,7 @@ "getattr", "setexec", "setfscreate", "noatsecure", "siginh", "setrlimit", "rlimitinh", "dyntransition", "setcurrent", "execmem", "execstack", "execheap", "setkeycreate", - "setsockcreate", "getrlimit", NULL } }, + "setsockcreate", "getrlimit", "setmempolicy", NULL } }, { "process2", { "nnp_transition", "nosuid_transition", NULL } }, { "system", From patchwork Fri Dec 8 09:06:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13485035 X-Patchwork-Delegate: bpf@iogearbox.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YuolUJgw" Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EC381730; Fri, 8 Dec 2023 01:06:49 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1d0538d9bbcso16774275ad.3; Fri, 08 Dec 2023 01:06:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702026409; x=1702631209; darn=vger.kernel.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=nU9j6PmyAYnwHf3ycIBsVG8xT88NU3yJVsd2qrFmMpI=; b=YuolUJgwG50jo9OqMQw63g7B8R2HuRF9bkS8yrqdR3r+y9XYXVcykhZHb/eheWpW+5 51ORqVWaUt8h/T+dQauHPO8CVlA475Hkte78hL9RnWmD9I1gSHSIim9iEdUgM/DLR3ip LvWXq7UApSxUqUc3gyydx+gvxM4bbGDSLQejH+CQTewaShPqlN4ra00TSr6HA3bTT3UM 2brwTfSQO6XQyUu0V+GSjz1r3ytSf6h/W4N/i9kA2TEltYqvZ2SdEwU7SCXKAkIejKn2 Ldps65E94dX0QzL09aDgPCQLNbUY5AfZTA4hPoe8v6UlBPjuBrGBg+YVDa5tzZN/NSOq 92wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702026409; x=1702631209; 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=nU9j6PmyAYnwHf3ycIBsVG8xT88NU3yJVsd2qrFmMpI=; b=EUYbm46bn416fjsDy1KRuElMAcVrm6wjbLAPhy4kDQUb8g1XZqYCUYdiOQatC9tmIX cNbMIWR3VezkoPr6tdPm2QYSmysSWm8rMKP3rppgirXr9iPSYDByHIimD2OCah2agrcK mooV1URIpk2qLwu9eWvwpMwgK1oweLlc/9OY4UaBRdFw9S25394/dH7cGGEblpDdOYuy 3LQawL3BskA5vwW1S5T9vPvjnkzMp2UAfmp3CsvQIMSd9asZARUIHv2nEDyhGa4IkWgI Ug991jbWX4OaNwgUrC/cm6vGRYfw623nj4hk/+g9joSLtO4NCHuI/J+q+fXJL5yNjrSx +ieQ== X-Gm-Message-State: AOJu0YzY85AFveseQWnSd95yzc9OeJlZJqEcGtUJeb6irqDMzn8l5f/8 I4bX64YMs++jOPilnfc8UUg= X-Google-Smtp-Source: AGHT+IFStUfBJJa8ImSvTVzgn8M/BHq8OdIdJjzD8odHwdrF3muYRwZVDrOvFB6m8GWXoMywE/1+TQ== X-Received: by 2002:a17:903:1d1:b0:1d0:bcb2:b914 with SMTP id e17-20020a17090301d100b001d0bcb2b914mr4703824plh.129.1702026408724; Fri, 08 Dec 2023 01:06:48 -0800 (PST) Received: from vultr.guest ([2001:19f0:ac00:4055:5400:4ff:fead:3bd0]) by smtp.gmail.com with ESMTPSA id 21-20020a170902ee5500b001d057080022sm1188173plo.20.2023.12.08.01.06.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 01:06:48 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, 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 v4 5/5] selftests/bpf: Add selftests for set_mempolicy with a lsm prog Date: Fri, 8 Dec 2023 09:06:22 +0000 Message-Id: <20231208090622.4309-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231208090622.4309-1-laoar.shao@gmail.com> References: <20231208090622.4309-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The result as follows, #263/1 set_mempolicy/MPOL_BIND_without_lsm:OK #263/2 set_mempolicy/MPOL_DEFAULT_without_lsm:OK #263/3 set_mempolicy/MPOL_BIND_with_lsm:OK #263/4 set_mempolicy/MPOL_DEFAULT_with_lsm:OK #263 set_mempolicy:OK Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao --- .../selftests/bpf/prog_tests/set_mempolicy.c | 81 ++++++++++++++++++++++ .../selftests/bpf/progs/test_set_mempolicy.c | 28 ++++++++ 2 files changed, 109 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/set_mempolicy.c create mode 100644 tools/testing/selftests/bpf/progs/test_set_mempolicy.c diff --git a/tools/testing/selftests/bpf/prog_tests/set_mempolicy.c b/tools/testing/selftests/bpf/prog_tests/set_mempolicy.c new file mode 100644 index 0000000..736b5e3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/set_mempolicy.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2023 Yafang Shao */ + +#include +#include +#include +#include +#include +#include "test_set_mempolicy.skel.h" + +#define SIZE 4096 + +static void mempolicy_bind(bool success) +{ + unsigned long mask = 1; + char *addr; + int err; + + addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (!ASSERT_OK_PTR(addr, "mmap")) + return; + + /* -lnuma is required by mbind(2), so use __NR_mbind to avoid the dependency. */ + err = syscall(__NR_mbind, addr, SIZE, MPOL_BIND, &mask, sizeof(mask), 0); + if (success) + ASSERT_OK(err, "mbind_success"); + else + ASSERT_ERR(err, "mbind_fail"); + + munmap(addr, SIZE); +} + +static void mempolicy_default(void) +{ + char *addr; + int err; + + addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (!ASSERT_OK_PTR(addr, "mmap")) + return; + + err = syscall(__NR_mbind, addr, SIZE, MPOL_DEFAULT, NULL, 0, 0); + ASSERT_OK(err, "mbind_success"); + + munmap(addr, SIZE); +} + +void test_set_mempolicy(void) +{ + struct test_set_mempolicy *skel; + int err; + + skel = test_set_mempolicy__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + + skel->bss->target_pid = getpid(); + + err = test_set_mempolicy__load(skel); + if (!ASSERT_OK(err, "load")) + goto destroy; + + if (test__start_subtest("MPOL_BIND_without_lsm")) + mempolicy_bind(true); + if (test__start_subtest("MPOL_DEFAULT_without_lsm")) + mempolicy_default(); + + /* Attach LSM prog first */ + err = test_set_mempolicy__attach(skel); + if (!ASSERT_OK(err, "attach")) + goto destroy; + + /* syscall to adjust memory policy */ + if (test__start_subtest("MPOL_BIND_with_lsm")) + mempolicy_bind(false); + if (test__start_subtest("MPOL_DEFAULT_with_lsm")) + mempolicy_default(); + +destroy: + test_set_mempolicy__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_set_mempolicy.c b/tools/testing/selftests/bpf/progs/test_set_mempolicy.c new file mode 100644 index 0000000..b5356d5 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_set_mempolicy.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2023 Yafang Shao */ + +#include "vmlinux.h" +#include +#include + +int target_pid; + +static int mem_policy_adjustment(u64 mode) +{ + struct task_struct *task = bpf_get_current_task_btf(); + + if (task->pid != target_pid) + return 0; + + if (mode != MPOL_BIND) + return 0; + return -1; +} + +SEC("lsm/set_mempolicy") +int BPF_PROG(setmempolicy, u64 mode, u16 mode_flags, nodemask_t *nmask, u32 flags) +{ + return mem_policy_adjustment(mode); +} + +char _license[] SEC("license") = "GPL";