概述
Linux内核提供了一种灵活的安全模块框架,使得开发人员可以方便地实现自定义的安全策略。其中一个重要的数据结构就是"security_operations
"。这个结构体定义在<linux/security.h>头文件中。
在Linux内核中,安全模块是一种动态加载的内核模块,用于实现自定义的安全策略。安全模块框架提供了一组安全钩子函数(security hooks),在内核执行关键操作时调用这些函数,以实现自定义安全策略的执行。
安全钩子函数是一组函数指针,它们指向安全模块所实现的自定义安全策略。当内核执行关键操作时,会调用相应的安全钩子函数,以检查是否允许该操作执行。如果安全钩子函数返回0,则表示允许该操作执行;如果返回非0,则表示拒绝该操作执行。
部分成员介绍
该结构体包含了多个安全钩子函数指针,这些指针用于注册安全模块的自定义安全策略。下面是部分成员的主要作用:
-
“name”:表示安全模块的名称,类型是"char *"。这个名称将在内核中用于唯一标识安全模块,并与其他安全模块区分开来。
-
“inode_permission”:表示对文件或目录进行权限检查时要执行的安全钩子函数,类型是"
int (*inode_permission)(struct inode *, int)
"。这个函数将在访问文件或目录时被调用,用于执行自定义的安全策略。 -
“file_permission”:表示对文件进行权限检查时要执行的安全钩子函数,类型是"
int (*file_permission)(struct file *, int)
"。这个函数将在打开文件时被调用,用于执行自定义的安全策略。 -
“task_create”:表示创建新进程时要执行的安全钩子函数,类型是"
int (*task_create)(unsigned long)
"。这个函数将在创建新进程时被调用,用于执行自定义的安全策略。 -
“cred_prepare”:表示准备新的进程凭证时要执行的安全钩子函数,类型是"
int (*cred_prepare)(struct cred *)
"。这个函数将在新的进程凭证被创建时被调用,用于执行自定义的安全策略。 -
“cred_transfer”:表示进程凭证转移时要执行的安全钩子函数,类型是"
void (*cred_transfer)(struct cred *, struct cred *)
"。这个函数将在进程凭证被转移时被调用,用于执行自定义的安全策略。 -
“sb_mount”:表示挂载文件系统时要执行的安全钩子函数,类型是"
int (*sb_mount)(const char *, const struct path *, const char *, unsigned long, void *)
"。这个函数将在文件系统挂载时被调用,用于执行自定义的安全策略。 -
“sb_umount”:表示卸载文件系统时要执行的安全钩子函数,类型是"
int (*sb_umount)(struct vfsmount *, int)
"。这个函数将在文件系统卸载时被调用,用于执行自定义的安全策略。 -
“bprm_check_security”:表示在执行可执行文件时要执行的安全钩子函数,类型是"
int (*bprm_check_security)(struct linux_binprm *)
"。这个函数将在执行可执行文件时被调用,用于执行自定义的安全策略。 -
“file_ioctl”:表示处理文件I/O控制命令时要执行的安全钩子函数,类型是"
long (*file_ioctl)(struct file *, unsigned int, unsigned long)
"。这个函数将在处理文件I/O控制命令时被调用,用于执行自定义的安全策略。
security_operations结构体源代码
1378 struct security_operations {
1379 char name[SECURITY_NAME_MAX + 1];
1380
1381 int (*ptrace_access_check) (struct task_struct *child, unsigned int mode);
1382 int (*ptrace_traceme) (struct task_struct *parent);
1383 int (*capget) (struct task_struct *target,
1384 kernel_cap_t *effective,
1385 kernel_cap_t *inheritable, kernel_cap_t *permitted);
1386 int (*capset) (struct cred *new,
1387 const struct cred *old,
1388 const kernel_cap_t *effective,
1389 const kernel_cap_t *inheritable,
1390 const kernel_cap_t *permitted);
1391 int (*capable) (struct task_struct *tsk, const struct cred *cred,
1392 struct user_namespace *ns, int cap, int audit);
1393 int (*quotactl) (int cmds, int type, int id, struct super_block *sb);
1394 int (*quota_on) (struct dentry *dentry);
1395 int (*syslog) (int type);
1396 int (*settime) (const struct timespec *ts, const struct timezone *tz);
1397 int (*vm_enough_memory) (struct mm_struct *mm, long pages);
1398
1399 int (*bprm_set_creds) (struct linux_binprm *bprm);
1400 int (*bprm_check_security) (struct linux_binprm *bprm);
1401 int (*bprm_secureexec) (struct linux_binprm *bprm);
1402 void (*bprm_committing_creds) (struct linux_binprm *bprm);
1403 void (*bprm_committed_creds) (struct linux_binprm *bprm);
1404
1405 int (*sb_alloc_security) (struct super_block *sb);
1406 void (*sb_free_security) (struct super_block *sb);
1407 int (*sb_copy_data) (char *orig, char *copy);
1408 int (*sb_remount) (struct super_block *sb, void *data);
1409 int (*sb_kern_mount) (struct super_block *sb, int flags, void *data);
1410 int (*sb_show_options) (struct seq_file *m, struct super_block *sb);
1411 int (*sb_statfs) (struct dentry *dentry);
1412 int (*sb_mount) (char *dev_name, struct path *path,
1413 char *type, unsigned long flags, void *data);
1414 int (*sb_umount) (struct vfsmount *mnt, int flags);
1415 int (*sb_pivotroot) (struct path *old_path,
1416 struct path *new_path);
1417 int (*sb_set_mnt_opts) (struct super_block *sb,
1418 struct security_mnt_opts *opts);
1419 void (*sb_clone_mnt_opts) (const struct super_block *oldsb,
1420 struct super_block *newsb);
1421 int (*sb_parse_opts_str) (char *options, struct security_mnt_opts *opts);
1422
1423 #ifdef CONFIG_SECURITY_PATH
1424 int (*path_unlink) (struct path *dir, struct dentry *dentry);
1425 int (*path_mkdir) (struct path *dir, struct dentry *dentry, int mode);
1426 int (*path_rmdir) (struct path *dir, struct dentry *dentry);
1427 int (*path_mknod) (struct path *dir, struct dentry *dentry, int mode,
1428 unsigned int dev);
1429 int (*path_truncate) (struct path *path);
1430 int (*path_symlink) (struct path *dir, struct dentry *dentry,
1431 const