2 * security/tomoyo/tomoyo.c
4 * LSM hooks for TOMOYO Linux.
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
8 * Version: 2.2.0 2009/04/01
12 #include <linux/security.h>
17 static int tomoyo_cred_prepare(struct cred
*new, const struct cred
*old
,
21 * Since "struct tomoyo_domain_info *" is a sharable pointer,
22 * we don't need to duplicate.
24 new->security
= old
->security
;
28 static int tomoyo_bprm_set_creds(struct linux_binprm
*bprm
)
32 rc
= cap_bprm_set_creds(bprm
);
37 * Do only if this function is called for the first time of an execve
40 if (bprm
->cred_prepared
)
43 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
46 if (!tomoyo_policy_loaded
)
47 tomoyo_load_policy(bprm
->filename
);
49 * Tell tomoyo_bprm_check_security() is called for the first time of an
52 bprm
->cred
->security
= NULL
;
56 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
58 struct tomoyo_domain_info
*domain
= bprm
->cred
->security
;
61 * Execute permission is checked against pathname passed to do_execve()
62 * using current domain.
65 struct tomoyo_domain_info
*next_domain
= NULL
;
66 int retval
= tomoyo_find_next_domain(bprm
, &next_domain
);
69 bprm
->cred
->security
= next_domain
;
73 * Read permission is checked against interpreters using next domain.
74 * '1' is the result of open_to_namei_flags(O_RDONLY).
76 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
, 1);
81 static int tomoyo_prepend(char **buffer
, int *buflen
, const char *str
)
83 int namelen
= strlen(str
);
85 if (*buflen
< namelen
)
89 memcpy(*buffer
, str
, namelen
);
94 * tomoyo_sysctl_path - return the realpath of a ctl_table.
95 * @table: pointer to "struct ctl_table".
97 * Returns realpath(3) of the @table on success.
98 * Returns NULL on failure.
100 * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
101 * if this function didn't return NULL.
103 static char *tomoyo_sysctl_path(struct ctl_table
*table
)
105 int buflen
= TOMOYO_MAX_PATHNAME_LEN
;
106 char *buf
= tomoyo_alloc(buflen
);
107 char *end
= buf
+ buflen
;
117 const char *sp
= table
->procname
;
120 memset(num
, 0, sizeof(num
));
121 snprintf(num
, sizeof(num
) - 1, "=%d=", table
->ctl_name
);
124 if (tomoyo_prepend(&end
, &buflen
, sp
) ||
125 tomoyo_prepend(&end
, &buflen
, "/"))
127 table
= table
->parent
;
129 if (tomoyo_prepend(&end
, &buflen
, "/proc/sys"))
131 error
= tomoyo_encode(buf
, end
- buf
, end
);
139 static int tomoyo_sysctl(struct ctl_table
*table
, int op
)
144 op
&= MAY_READ
| MAY_WRITE
;
147 name
= tomoyo_sysctl_path(table
);
150 error
= tomoyo_check_file_perm(tomoyo_domain(), name
, op
);
156 static int tomoyo_path_truncate(struct path
*path
, loff_t length
,
157 unsigned int time_attrs
)
159 return tomoyo_check_1path_perm(tomoyo_domain(),
160 TOMOYO_TYPE_TRUNCATE_ACL
,
164 static int tomoyo_path_unlink(struct path
*parent
, struct dentry
*dentry
)
166 struct path path
= { parent
->mnt
, dentry
};
167 return tomoyo_check_1path_perm(tomoyo_domain(),
168 TOMOYO_TYPE_UNLINK_ACL
,
172 static int tomoyo_path_mkdir(struct path
*parent
, struct dentry
*dentry
,
175 struct path path
= { parent
->mnt
, dentry
};
176 return tomoyo_check_1path_perm(tomoyo_domain(),
177 TOMOYO_TYPE_MKDIR_ACL
,
181 static int tomoyo_path_rmdir(struct path
*parent
, struct dentry
*dentry
)
183 struct path path
= { parent
->mnt
, dentry
};
184 return tomoyo_check_1path_perm(tomoyo_domain(),
185 TOMOYO_TYPE_RMDIR_ACL
,
189 static int tomoyo_path_symlink(struct path
*parent
, struct dentry
*dentry
,
190 const char *old_name
)
192 struct path path
= { parent
->mnt
, dentry
};
193 return tomoyo_check_1path_perm(tomoyo_domain(),
194 TOMOYO_TYPE_SYMLINK_ACL
,
198 static int tomoyo_path_mknod(struct path
*parent
, struct dentry
*dentry
,
199 int mode
, unsigned int dev
)
201 struct path path
= { parent
->mnt
, dentry
};
202 int type
= TOMOYO_TYPE_CREATE_ACL
;
204 switch (mode
& S_IFMT
) {
206 type
= TOMOYO_TYPE_MKCHAR_ACL
;
209 type
= TOMOYO_TYPE_MKBLOCK_ACL
;
212 type
= TOMOYO_TYPE_MKFIFO_ACL
;
215 type
= TOMOYO_TYPE_MKSOCK_ACL
;
218 return tomoyo_check_1path_perm(tomoyo_domain(),
222 static int tomoyo_path_link(struct dentry
*old_dentry
, struct path
*new_dir
,
223 struct dentry
*new_dentry
)
225 struct path path1
= { new_dir
->mnt
, old_dentry
};
226 struct path path2
= { new_dir
->mnt
, new_dentry
};
227 return tomoyo_check_2path_perm(tomoyo_domain(),
228 TOMOYO_TYPE_LINK_ACL
,
232 static int tomoyo_path_rename(struct path
*old_parent
,
233 struct dentry
*old_dentry
,
234 struct path
*new_parent
,
235 struct dentry
*new_dentry
)
237 struct path path1
= { old_parent
->mnt
, old_dentry
};
238 struct path path2
= { new_parent
->mnt
, new_dentry
};
239 return tomoyo_check_2path_perm(tomoyo_domain(),
240 TOMOYO_TYPE_RENAME_ACL
,
244 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
247 if (cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
))
248 return tomoyo_check_rewrite_permission(tomoyo_domain(), file
);
252 static int tomoyo_dentry_open(struct file
*f
, const struct cred
*cred
)
254 int flags
= f
->f_flags
;
256 if ((flags
+ 1) & O_ACCMODE
)
258 flags
|= f
->f_flags
& (O_APPEND
| O_TRUNC
);
259 /* Don't check read permission here if called from do_execve(). */
260 if (current
->in_execve
)
262 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
265 static struct security_operations tomoyo_security_ops
= {
267 .cred_prepare
= tomoyo_cred_prepare
,
268 .bprm_set_creds
= tomoyo_bprm_set_creds
,
269 .bprm_check_security
= tomoyo_bprm_check_security
,
271 .sysctl
= tomoyo_sysctl
,
273 .file_fcntl
= tomoyo_file_fcntl
,
274 .dentry_open
= tomoyo_dentry_open
,
275 .path_truncate
= tomoyo_path_truncate
,
276 .path_unlink
= tomoyo_path_unlink
,
277 .path_mkdir
= tomoyo_path_mkdir
,
278 .path_rmdir
= tomoyo_path_rmdir
,
279 .path_symlink
= tomoyo_path_symlink
,
280 .path_mknod
= tomoyo_path_mknod
,
281 .path_link
= tomoyo_path_link
,
282 .path_rename
= tomoyo_path_rename
,
285 static int __init
tomoyo_init(void)
287 struct cred
*cred
= (struct cred
*) current_cred();
289 if (!security_module_enable(&tomoyo_security_ops
))
291 /* register ourselves with the security framework */
292 if (register_security(&tomoyo_security_ops
))
293 panic("Failure registering TOMOYO Linux");
294 printk(KERN_INFO
"TOMOYO Linux initialized\n");
295 cred
->security
= &tomoyo_kernel_domain
;
296 tomoyo_realpath_init();
300 security_initcall(tomoyo_init
);