Skip to content

Commit 5f061d4

Browse files
laoarKernel Patches Daemon
authored andcommitted
mm, security: Add lsm hook for memory policy adjustment
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 <[email protected]>
1 parent f9b6239 commit 5f061d4

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

include/linux/lsm_hook_defs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,6 @@ LSM_HOOK(int, 0, uring_override_creds, const struct cred *new)
419419
LSM_HOOK(int, 0, uring_sqpoll, void)
420420
LSM_HOOK(int, 0, uring_cmd, struct io_uring_cmd *ioucmd)
421421
#endif /* CONFIG_IO_URING */
422+
423+
LSM_HOOK(int, 0, set_mempolicy, unsigned long mode, unsigned short mode_flags,
424+
nodemask_t *nmask, unsigned int flags)

include/linux/security.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen);
484484
int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen);
485485
int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen);
486486
int security_locked_down(enum lockdown_reason what);
487+
int security_set_mempolicy(unsigned long mode, unsigned short mode_flags,
488+
nodemask_t *nmask, unsigned int flags);
487489
#else /* CONFIG_SECURITY */
488490

489491
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)
13951397
{
13961398
return 0;
13971399
}
1400+
1401+
static inline int
1402+
security_set_mempolicy(unsigned long mode, unsigned short mode_flags,
1403+
nodemask_t *nmask, unsigned int flags)
1404+
{
1405+
return 0;
1406+
}
13981407
#endif /* CONFIG_SECURITY */
13991408

14001409
#if defined(CONFIG_SECURITY) && defined(CONFIG_WATCH_QUEUE)

mm/mempolicy.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,10 @@ static long kernel_mbind(unsigned long start, unsigned long len,
14831483
if (err)
14841484
return err;
14851485

1486+
err = security_set_mempolicy(lmode, mode_flags, &nodes, flags);
1487+
if (err)
1488+
return err;
1489+
14861490
return do_mbind(start, len, lmode, mode_flags, &nodes, flags);
14871491
}
14881492

@@ -1577,6 +1581,10 @@ static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask,
15771581
if (err)
15781582
return err;
15791583

1584+
err = security_set_mempolicy(lmode, mode_flags, &nodes, 0);
1585+
if (err)
1586+
return err;
1587+
15801588
return do_set_mempolicy(lmode, mode_flags, &nodes);
15811589
}
15821590

security/security.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5337,3 +5337,16 @@ int security_uring_cmd(struct io_uring_cmd *ioucmd)
53375337
return call_int_hook(uring_cmd, 0, ioucmd);
53385338
}
53395339
#endif /* CONFIG_IO_URING */
5340+
5341+
/**
5342+
* security_set_mempolicy() - Check if memory policy can be adjusted
5343+
* @mode: The memory policy mode to be set
5344+
* @mode_flags: optional mode flags
5345+
* @nmask: modemask to which the mode applies
5346+
* @flags: mode flags for mbind(2) only
5347+
*/
5348+
int security_set_mempolicy(unsigned long mode, unsigned short mode_flags,
5349+
nodemask_t *nmask, unsigned int flags)
5350+
{
5351+
return call_int_hook(set_mempolicy, 0, mode, mode_flags, nmask, flags);
5352+
}

0 commit comments

Comments
 (0)