| 1 |
/* |
| 2 |
* security/caitsith/lsm2caitsith.c |
| 3 |
* |
| 4 |
* Copyright (C) 2005-2012 NTT DATA CORPORATION |
| 5 |
* |
| 6 |
* Version: 0.2.3 2018/04/01 |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/path.h> |
| 10 |
#include <linux/security.h> |
| 11 |
#include <linux/caitsith.h> |
| 12 |
|
| 13 |
int ccs_sb_umount(struct vfsmount *mnt, int flags) |
| 14 |
{ |
| 15 |
return ccs_umount_permission(mnt, flags); |
| 16 |
} |
| 17 |
|
| 18 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0) |
| 19 |
int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) |
| 20 |
{ |
| 21 |
return ccs_getattr_permission(mnt, dentry); |
| 22 |
} |
| 23 |
#else |
| 24 |
int ccs_inode_getattr(const struct path *path) |
| 25 |
{ |
| 26 |
return ccs_getattr_permission(path->mnt, path->dentry); |
| 27 |
} |
| 28 |
#endif |
| 29 |
|
| 30 |
int ccs_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 31 |
{ |
| 32 |
return ccs_ioctl_permission(file, cmd, arg); |
| 33 |
} |
| 34 |
|
| 35 |
int ccs_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg) |
| 36 |
{ |
| 37 |
return ccs_fcntl_permission(file, cmd, arg); |
| 38 |
} |
| 39 |
|
| 40 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) |
| 41 |
int ccs_file_open(struct file *file) |
| 42 |
{ |
| 43 |
return ccs_open_permission(file); |
| 44 |
} |
| 45 |
#else |
| 46 |
int ccs_file_open(struct file *file, const struct cred *cred) |
| 47 |
{ |
| 48 |
return ccs_open_permission(file); |
| 49 |
} |
| 50 |
#endif |
| 51 |
|
| 52 |
int ccs_socket_create(int family, int type, int protocol, int kern) |
| 53 |
{ |
| 54 |
return ccs_socket_create_permission(family, type, protocol); |
| 55 |
} |
| 56 |
|
| 57 |
int ccs_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 58 |
{ |
| 59 |
return ccs_socket_bind_permission(sock, address, addrlen); |
| 60 |
} |
| 61 |
|
| 62 |
int ccs_socket_connect(struct socket *sock, struct sockaddr *address, |
| 63 |
int addrlen) |
| 64 |
{ |
| 65 |
return ccs_socket_connect_permission(sock, address, addrlen); |
| 66 |
} |
| 67 |
|
| 68 |
int ccs_socket_listen(struct socket *sock, int backlog) |
| 69 |
{ |
| 70 |
return ccs_socket_listen_permission(sock); |
| 71 |
} |
| 72 |
|
| 73 |
int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size) |
| 74 |
{ |
| 75 |
return ccs_socket_sendmsg_permission(sock, msg, size); |
| 76 |
} |
| 77 |
|
| 78 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) |
| 79 |
|
| 80 |
int ccs_settime(const struct timespec64 *ts, const struct timezone *tz) |
| 81 |
{ |
| 82 |
return ccs_capable(CCS_SYS_SETTIME) ? 0 : -EPERM; |
| 83 |
} |
| 84 |
|
| 85 |
int ccs_sb_mount(const char *dev_name, const struct path *path, |
| 86 |
const char *type, unsigned long flags, void *data) |
| 87 |
{ |
| 88 |
return ccs_mount_permission(dev_name, path, type, flags, data); |
| 89 |
} |
| 90 |
|
| 91 |
int ccs_sb_pivotroot(const struct path *old_path, const struct path *new_path) |
| 92 |
{ |
| 93 |
return ccs_pivot_root_permission(old_path, new_path); |
| 94 |
} |
| 95 |
|
| 96 |
int ccs_path_unlink(const struct path *dir, struct dentry *dentry) |
| 97 |
{ |
| 98 |
return ccs_unlink_permission(dentry, dir->mnt); |
| 99 |
} |
| 100 |
|
| 101 |
int ccs_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode) |
| 102 |
{ |
| 103 |
return ccs_mkdir_permission(dentry, dir->mnt, mode); |
| 104 |
} |
| 105 |
|
| 106 |
int ccs_path_rmdir(const struct path *dir, struct dentry *dentry) |
| 107 |
{ |
| 108 |
return ccs_rmdir_permission(dentry, dir->mnt); |
| 109 |
} |
| 110 |
|
| 111 |
int ccs_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode, |
| 112 |
unsigned int dev) |
| 113 |
{ |
| 114 |
return ccs_mknod_permission(dentry, dir->mnt, mode, dev); |
| 115 |
} |
| 116 |
|
| 117 |
int ccs_path_truncate(const struct path *path) |
| 118 |
{ |
| 119 |
return ccs_truncate_permission(path->dentry, path->mnt); |
| 120 |
} |
| 121 |
|
| 122 |
int ccs_path_symlink(const struct path *dir, struct dentry *dentry, |
| 123 |
const char *old_name) |
| 124 |
{ |
| 125 |
return ccs_symlink_permission(dentry, dir->mnt, old_name); |
| 126 |
} |
| 127 |
|
| 128 |
int ccs_path_link(struct dentry *old_dentry, const struct path *new_dir, |
| 129 |
struct dentry *new_dentry) |
| 130 |
{ |
| 131 |
return ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); |
| 132 |
} |
| 133 |
|
| 134 |
int ccs_path_rename(const struct path *old_dir, struct dentry *old_dentry, |
| 135 |
const struct path *new_dir, struct dentry *new_dentry) |
| 136 |
{ |
| 137 |
return ccs_rename_permission(old_dentry, new_dentry, new_dir->mnt); |
| 138 |
} |
| 139 |
|
| 140 |
int ccs_path_chmod(const struct path *path, umode_t mode) |
| 141 |
{ |
| 142 |
return ccs_chmod_permission(path->dentry, path->mnt, mode); |
| 143 |
} |
| 144 |
|
| 145 |
int ccs_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
| 146 |
{ |
| 147 |
return ccs_chown_permission(path->dentry, path->mnt, uid, gid); |
| 148 |
} |
| 149 |
|
| 150 |
int ccs_path_chroot(const struct path *path) |
| 151 |
{ |
| 152 |
return ccs_chroot_permission(path); |
| 153 |
} |
| 154 |
|
| 155 |
#else |
| 156 |
|
| 157 |
int ccs_settime(const struct timespec *ts, const struct timezone *tz) |
| 158 |
{ |
| 159 |
return ccs_capable(CCS_SYS_SETTIME) ? 0 : -EPERM; |
| 160 |
} |
| 161 |
|
| 162 |
int ccs_sb_mount(const char *dev_name, struct path *path, const char *type, |
| 163 |
unsigned long flags, void *data) |
| 164 |
{ |
| 165 |
return ccs_mount_permission(dev_name, path, type, flags, data); |
| 166 |
} |
| 167 |
|
| 168 |
int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) |
| 169 |
{ |
| 170 |
return ccs_pivot_root_permission(old_path, new_path); |
| 171 |
} |
| 172 |
|
| 173 |
int ccs_path_unlink(struct path *dir, struct dentry *dentry) |
| 174 |
{ |
| 175 |
return ccs_unlink_permission(dentry, dir->mnt); |
| 176 |
} |
| 177 |
|
| 178 |
int ccs_path_mkdir(struct path *dir, struct dentry *dentry, umode_t mode) |
| 179 |
{ |
| 180 |
return ccs_mkdir_permission(dentry, dir->mnt, mode); |
| 181 |
} |
| 182 |
|
| 183 |
int ccs_path_rmdir(struct path *dir, struct dentry *dentry) |
| 184 |
{ |
| 185 |
return ccs_rmdir_permission(dentry, dir->mnt); |
| 186 |
} |
| 187 |
|
| 188 |
int ccs_path_mknod(struct path *dir, struct dentry *dentry, umode_t mode, |
| 189 |
unsigned int dev) |
| 190 |
{ |
| 191 |
return ccs_mknod_permission(dentry, dir->mnt, mode, dev); |
| 192 |
} |
| 193 |
|
| 194 |
int ccs_path_truncate(struct path *path) |
| 195 |
{ |
| 196 |
return ccs_truncate_permission(path->dentry, path->mnt); |
| 197 |
} |
| 198 |
|
| 199 |
int ccs_path_symlink(struct path *dir, struct dentry *dentry, |
| 200 |
const char *old_name) |
| 201 |
{ |
| 202 |
return ccs_symlink_permission(dentry, dir->mnt, old_name); |
| 203 |
} |
| 204 |
|
| 205 |
int ccs_path_link(struct dentry *old_dentry, struct path *new_dir, |
| 206 |
struct dentry *new_dentry) |
| 207 |
{ |
| 208 |
return ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); |
| 209 |
} |
| 210 |
|
| 211 |
int ccs_path_rename(struct path *old_dir, struct dentry *old_dentry, |
| 212 |
struct path *new_dir, struct dentry *new_dentry) |
| 213 |
{ |
| 214 |
return ccs_rename_permission(old_dentry, new_dentry, new_dir->mnt); |
| 215 |
} |
| 216 |
|
| 217 |
int ccs_path_chmod(struct path *path, umode_t mode) |
| 218 |
{ |
| 219 |
return ccs_chmod_permission(path->dentry, path->mnt, mode); |
| 220 |
} |
| 221 |
|
| 222 |
int ccs_path_chown(struct path *path, kuid_t uid, kgid_t gid) |
| 223 |
{ |
| 224 |
return ccs_chown_permission(path->dentry, path->mnt, uid, gid); |
| 225 |
} |
| 226 |
|
| 227 |
int ccs_path_chroot(struct path *path) |
| 228 |
{ |
| 229 |
return ccs_chroot_permission(path); |
| 230 |
} |
| 231 |
|
| 232 |
#endif |
| 233 |
|
| 234 |
#if !defined(CONFIG_SECURITY_PATH) |
| 235 |
EXPORT_SYMBOL(ccs_path_mkdir); |
| 236 |
EXPORT_SYMBOL(ccs_path_mknod); |
| 237 |
EXPORT_SYMBOL(ccs_path_unlink); |
| 238 |
EXPORT_SYMBOL(ccs_path_rename); |
| 239 |
#endif |
| 240 |
|
| 241 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY) |
| 242 |
|
| 243 |
#include <linux/lsm_hooks.h> |
| 244 |
|
| 245 |
static struct security_hook_list caitsith_hooks[] = { |
| 246 |
LSM_HOOK_INIT(settime, ccs_settime), |
| 247 |
LSM_HOOK_INIT(sb_mount, ccs_sb_mount), |
| 248 |
LSM_HOOK_INIT(sb_umount, ccs_sb_umount), |
| 249 |
LSM_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot), |
| 250 |
LSM_HOOK_INIT(inode_getattr, ccs_inode_getattr), |
| 251 |
LSM_HOOK_INIT(file_ioctl, ccs_file_ioctl), |
| 252 |
LSM_HOOK_INIT(file_fcntl, ccs_file_fcntl), |
| 253 |
LSM_HOOK_INIT(file_open, ccs_file_open), |
| 254 |
#if defined(CONFIG_SECURITY_NETWORK) |
| 255 |
LSM_HOOK_INIT(socket_create, ccs_socket_create), |
| 256 |
LSM_HOOK_INIT(socket_bind, ccs_socket_bind), |
| 257 |
LSM_HOOK_INIT(socket_connect, ccs_socket_connect), |
| 258 |
LSM_HOOK_INIT(socket_listen, ccs_socket_listen), |
| 259 |
LSM_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg), |
| 260 |
#endif |
| 261 |
#if defined(CONFIG_SECURITY_PATH) |
| 262 |
LSM_HOOK_INIT(path_unlink, ccs_path_unlink), |
| 263 |
LSM_HOOK_INIT(path_mkdir, ccs_path_mkdir), |
| 264 |
LSM_HOOK_INIT(path_rmdir, ccs_path_rmdir), |
| 265 |
LSM_HOOK_INIT(path_mknod, ccs_path_mknod), |
| 266 |
LSM_HOOK_INIT(path_truncate, ccs_path_truncate), |
| 267 |
LSM_HOOK_INIT(path_symlink, ccs_path_symlink), |
| 268 |
LSM_HOOK_INIT(path_link, ccs_path_link), |
| 269 |
LSM_HOOK_INIT(path_rename, ccs_path_rename), |
| 270 |
LSM_HOOK_INIT(path_chmod, ccs_path_chmod), |
| 271 |
LSM_HOOK_INIT(path_chown, ccs_path_chown), |
| 272 |
LSM_HOOK_INIT(path_chroot, ccs_path_chroot), |
| 273 |
#endif |
| 274 |
}; |
| 275 |
|
| 276 |
static int __init cs_add_hooks(void) |
| 277 |
{ |
| 278 |
if (caitsith_ops.disabled) |
| 279 |
return 0; |
| 280 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) |
| 281 |
security_add_hooks(caitsith_hooks, ARRAY_SIZE(caitsith_hooks), |
| 282 |
"caitsith"); |
| 283 |
#else |
| 284 |
security_add_hooks(caitsith_hooks, ARRAY_SIZE(caitsith_hooks)); |
| 285 |
#endif |
| 286 |
return 0; |
| 287 |
} |
| 288 |
late_initcall(cs_add_hooks); |
| 289 |
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY) */ |