Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Simplified MAC Kernel (smack) security module |
| 3 | * |
| 4 | * This file contains the smack hook function implementations. |
| 5 | * |
| 6 | * Author: |
| 7 | * Casey Schaufler <casey@schaufler-ca.com> |
| 8 | * |
| 9 | * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2, |
| 13 | * as published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/xattr.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/mount.h> |
| 19 | #include <linux/stat.h> |
| 20 | #include <linux/ext2_fs.h> |
| 21 | #include <linux/kd.h> |
| 22 | #include <asm/ioctls.h> |
| 23 | #include <linux/tcp.h> |
| 24 | #include <linux/udp.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/pipe_fs_i.h> |
| 27 | #include <net/netlabel.h> |
| 28 | #include <net/cipso_ipv4.h> |
| 29 | |
| 30 | #include "smack.h" |
| 31 | |
| 32 | /* |
| 33 | * I hope these are the hokeyist lines of code in the module. Casey. |
| 34 | */ |
| 35 | #define DEVPTS_SUPER_MAGIC 0x1cd1 |
| 36 | #define SOCKFS_MAGIC 0x534F434B |
| 37 | #define TMPFS_MAGIC 0x01021994 |
| 38 | |
| 39 | /** |
| 40 | * smk_fetch - Fetch the smack label from a file. |
| 41 | * @ip: a pointer to the inode |
| 42 | * @dp: a pointer to the dentry |
| 43 | * |
| 44 | * Returns a pointer to the master list entry for the Smack label |
| 45 | * or NULL if there was no label to fetch. |
| 46 | */ |
| 47 | static char *smk_fetch(struct inode *ip, struct dentry *dp) |
| 48 | { |
| 49 | int rc; |
| 50 | char in[SMK_LABELLEN]; |
| 51 | |
| 52 | if (ip->i_op->getxattr == NULL) |
| 53 | return NULL; |
| 54 | |
| 55 | rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN); |
| 56 | if (rc < 0) |
| 57 | return NULL; |
| 58 | |
| 59 | return smk_import(in, rc); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * new_inode_smack - allocate an inode security blob |
| 64 | * @smack: a pointer to the Smack label to use in the blob |
| 65 | * |
| 66 | * Returns the new blob or NULL if there's no memory available |
| 67 | */ |
| 68 | struct inode_smack *new_inode_smack(char *smack) |
| 69 | { |
| 70 | struct inode_smack *isp; |
| 71 | |
| 72 | isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL); |
| 73 | if (isp == NULL) |
| 74 | return NULL; |
| 75 | |
| 76 | isp->smk_inode = smack; |
| 77 | isp->smk_flags = 0; |
| 78 | mutex_init(&isp->smk_lock); |
| 79 | |
| 80 | return isp; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * LSM hooks. |
| 85 | * We he, that is fun! |
| 86 | */ |
| 87 | |
| 88 | /** |
| 89 | * smack_ptrace - Smack approval on ptrace |
| 90 | * @ptp: parent task pointer |
| 91 | * @ctp: child task pointer |
| 92 | * |
| 93 | * Returns 0 if access is OK, an error code otherwise |
| 94 | * |
| 95 | * Do the capability checks, and require read and write. |
| 96 | */ |
| 97 | static int smack_ptrace(struct task_struct *ptp, struct task_struct *ctp) |
| 98 | { |
| 99 | int rc; |
| 100 | |
| 101 | rc = cap_ptrace(ptp, ctp); |
| 102 | if (rc != 0) |
| 103 | return rc; |
| 104 | |
| 105 | rc = smk_access(ptp->security, ctp->security, MAY_READWRITE); |
| 106 | if (rc != 0 && __capable(ptp, CAP_MAC_OVERRIDE)) |
| 107 | return 0; |
| 108 | |
| 109 | return rc; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * smack_syslog - Smack approval on syslog |
| 114 | * @type: message type |
| 115 | * |
| 116 | * Require that the task has the floor label |
| 117 | * |
| 118 | * Returns 0 on success, error code otherwise. |
| 119 | */ |
| 120 | static int smack_syslog(int type) |
| 121 | { |
| 122 | int rc; |
| 123 | char *sp = current->security; |
| 124 | |
| 125 | rc = cap_syslog(type); |
| 126 | if (rc != 0) |
| 127 | return rc; |
| 128 | |
| 129 | if (capable(CAP_MAC_OVERRIDE)) |
| 130 | return 0; |
| 131 | |
| 132 | if (sp != smack_known_floor.smk_known) |
| 133 | rc = -EACCES; |
| 134 | |
| 135 | return rc; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Superblock Hooks. |
| 141 | */ |
| 142 | |
| 143 | /** |
| 144 | * smack_sb_alloc_security - allocate a superblock blob |
| 145 | * @sb: the superblock getting the blob |
| 146 | * |
| 147 | * Returns 0 on success or -ENOMEM on error. |
| 148 | */ |
| 149 | static int smack_sb_alloc_security(struct super_block *sb) |
| 150 | { |
| 151 | struct superblock_smack *sbsp; |
| 152 | |
| 153 | sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL); |
| 154 | |
| 155 | if (sbsp == NULL) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | sbsp->smk_root = smack_known_floor.smk_known; |
| 159 | sbsp->smk_default = smack_known_floor.smk_known; |
| 160 | sbsp->smk_floor = smack_known_floor.smk_known; |
| 161 | sbsp->smk_hat = smack_known_hat.smk_known; |
| 162 | sbsp->smk_initialized = 0; |
| 163 | spin_lock_init(&sbsp->smk_sblock); |
| 164 | |
| 165 | sb->s_security = sbsp; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * smack_sb_free_security - free a superblock blob |
| 172 | * @sb: the superblock getting the blob |
| 173 | * |
| 174 | */ |
| 175 | static void smack_sb_free_security(struct super_block *sb) |
| 176 | { |
| 177 | kfree(sb->s_security); |
| 178 | sb->s_security = NULL; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * smack_sb_copy_data - copy mount options data for processing |
| 183 | * @type: file system type |
| 184 | * @orig: where to start |
| 185 | * @smackopts |
| 186 | * |
| 187 | * Returns 0 on success or -ENOMEM on error. |
| 188 | * |
| 189 | * Copy the Smack specific mount options out of the mount |
| 190 | * options list. |
| 191 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 192 | static int smack_sb_copy_data(char *orig, char *smackopts) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 193 | { |
| 194 | char *cp, *commap, *otheropts, *dp; |
| 195 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 196 | otheropts = (char *)get_zeroed_page(GFP_KERNEL); |
| 197 | if (otheropts == NULL) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) { |
| 201 | if (strstr(cp, SMK_FSDEFAULT) == cp) |
| 202 | dp = smackopts; |
| 203 | else if (strstr(cp, SMK_FSFLOOR) == cp) |
| 204 | dp = smackopts; |
| 205 | else if (strstr(cp, SMK_FSHAT) == cp) |
| 206 | dp = smackopts; |
| 207 | else if (strstr(cp, SMK_FSROOT) == cp) |
| 208 | dp = smackopts; |
| 209 | else |
| 210 | dp = otheropts; |
| 211 | |
| 212 | commap = strchr(cp, ','); |
| 213 | if (commap != NULL) |
| 214 | *commap = '\0'; |
| 215 | |
| 216 | if (*dp != '\0') |
| 217 | strcat(dp, ","); |
| 218 | strcat(dp, cp); |
| 219 | } |
| 220 | |
| 221 | strcpy(orig, otheropts); |
| 222 | free_page((unsigned long)otheropts); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * smack_sb_kern_mount - Smack specific mount processing |
| 229 | * @sb: the file system superblock |
| 230 | * @data: the smack mount options |
| 231 | * |
| 232 | * Returns 0 on success, an error code on failure |
| 233 | */ |
| 234 | static int smack_sb_kern_mount(struct super_block *sb, void *data) |
| 235 | { |
| 236 | struct dentry *root = sb->s_root; |
| 237 | struct inode *inode = root->d_inode; |
| 238 | struct superblock_smack *sp = sb->s_security; |
| 239 | struct inode_smack *isp; |
| 240 | char *op; |
| 241 | char *commap; |
| 242 | char *nsp; |
| 243 | |
| 244 | spin_lock(&sp->smk_sblock); |
| 245 | if (sp->smk_initialized != 0) { |
| 246 | spin_unlock(&sp->smk_sblock); |
| 247 | return 0; |
| 248 | } |
| 249 | sp->smk_initialized = 1; |
| 250 | spin_unlock(&sp->smk_sblock); |
| 251 | |
| 252 | for (op = data; op != NULL; op = commap) { |
| 253 | commap = strchr(op, ','); |
| 254 | if (commap != NULL) |
| 255 | *commap++ = '\0'; |
| 256 | |
| 257 | if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) { |
| 258 | op += strlen(SMK_FSHAT); |
| 259 | nsp = smk_import(op, 0); |
| 260 | if (nsp != NULL) |
| 261 | sp->smk_hat = nsp; |
| 262 | } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) { |
| 263 | op += strlen(SMK_FSFLOOR); |
| 264 | nsp = smk_import(op, 0); |
| 265 | if (nsp != NULL) |
| 266 | sp->smk_floor = nsp; |
| 267 | } else if (strncmp(op, SMK_FSDEFAULT, |
| 268 | strlen(SMK_FSDEFAULT)) == 0) { |
| 269 | op += strlen(SMK_FSDEFAULT); |
| 270 | nsp = smk_import(op, 0); |
| 271 | if (nsp != NULL) |
| 272 | sp->smk_default = nsp; |
| 273 | } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) { |
| 274 | op += strlen(SMK_FSROOT); |
| 275 | nsp = smk_import(op, 0); |
| 276 | if (nsp != NULL) |
| 277 | sp->smk_root = nsp; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Initialize the root inode. |
| 283 | */ |
| 284 | isp = inode->i_security; |
| 285 | if (isp == NULL) |
| 286 | inode->i_security = new_inode_smack(sp->smk_root); |
| 287 | else |
| 288 | isp->smk_inode = sp->smk_root; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * smack_sb_statfs - Smack check on statfs |
| 295 | * @dentry: identifies the file system in question |
| 296 | * |
| 297 | * Returns 0 if current can read the floor of the filesystem, |
| 298 | * and error code otherwise |
| 299 | */ |
| 300 | static int smack_sb_statfs(struct dentry *dentry) |
| 301 | { |
| 302 | struct superblock_smack *sbp = dentry->d_sb->s_security; |
| 303 | |
| 304 | return smk_curacc(sbp->smk_floor, MAY_READ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * smack_sb_mount - Smack check for mounting |
| 309 | * @dev_name: unused |
| 310 | * @nd: mount point |
| 311 | * @type: unused |
| 312 | * @flags: unused |
| 313 | * @data: unused |
| 314 | * |
| 315 | * Returns 0 if current can write the floor of the filesystem |
| 316 | * being mounted on, an error code otherwise. |
| 317 | */ |
Al Viro | b5266eb | 2008-03-22 17:48:24 -0400 | [diff] [blame] | 318 | static int smack_sb_mount(char *dev_name, struct path *path, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 319 | char *type, unsigned long flags, void *data) |
| 320 | { |
Al Viro | b5266eb | 2008-03-22 17:48:24 -0400 | [diff] [blame] | 321 | struct superblock_smack *sbp = path->mnt->mnt_sb->s_security; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 322 | |
| 323 | return smk_curacc(sbp->smk_floor, MAY_WRITE); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * smack_sb_umount - Smack check for unmounting |
| 328 | * @mnt: file system to unmount |
| 329 | * @flags: unused |
| 330 | * |
| 331 | * Returns 0 if current can write the floor of the filesystem |
| 332 | * being unmounted, an error code otherwise. |
| 333 | */ |
| 334 | static int smack_sb_umount(struct vfsmount *mnt, int flags) |
| 335 | { |
| 336 | struct superblock_smack *sbp; |
| 337 | |
| 338 | sbp = mnt->mnt_sb->s_security; |
| 339 | |
| 340 | return smk_curacc(sbp->smk_floor, MAY_WRITE); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Inode hooks |
| 345 | */ |
| 346 | |
| 347 | /** |
| 348 | * smack_inode_alloc_security - allocate an inode blob |
| 349 | * @inode - the inode in need of a blob |
| 350 | * |
| 351 | * Returns 0 if it gets a blob, -ENOMEM otherwise |
| 352 | */ |
| 353 | static int smack_inode_alloc_security(struct inode *inode) |
| 354 | { |
| 355 | inode->i_security = new_inode_smack(current->security); |
| 356 | if (inode->i_security == NULL) |
| 357 | return -ENOMEM; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * smack_inode_free_security - free an inode blob |
| 363 | * @inode - the inode with a blob |
| 364 | * |
| 365 | * Clears the blob pointer in inode |
| 366 | */ |
| 367 | static void smack_inode_free_security(struct inode *inode) |
| 368 | { |
| 369 | kfree(inode->i_security); |
| 370 | inode->i_security = NULL; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * smack_inode_init_security - copy out the smack from an inode |
| 375 | * @inode: the inode |
| 376 | * @dir: unused |
| 377 | * @name: where to put the attribute name |
| 378 | * @value: where to put the attribute value |
| 379 | * @len: where to put the length of the attribute |
| 380 | * |
| 381 | * Returns 0 if it all works out, -ENOMEM if there's no memory |
| 382 | */ |
| 383 | static int smack_inode_init_security(struct inode *inode, struct inode *dir, |
| 384 | char **name, void **value, size_t *len) |
| 385 | { |
| 386 | char *isp = smk_of_inode(inode); |
| 387 | |
| 388 | if (name) { |
| 389 | *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL); |
| 390 | if (*name == NULL) |
| 391 | return -ENOMEM; |
| 392 | } |
| 393 | |
| 394 | if (value) { |
| 395 | *value = kstrdup(isp, GFP_KERNEL); |
| 396 | if (*value == NULL) |
| 397 | return -ENOMEM; |
| 398 | } |
| 399 | |
| 400 | if (len) |
| 401 | *len = strlen(isp) + 1; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * smack_inode_link - Smack check on link |
| 408 | * @old_dentry: the existing object |
| 409 | * @dir: unused |
| 410 | * @new_dentry: the new object |
| 411 | * |
| 412 | * Returns 0 if access is permitted, an error code otherwise |
| 413 | */ |
| 414 | static int smack_inode_link(struct dentry *old_dentry, struct inode *dir, |
| 415 | struct dentry *new_dentry) |
| 416 | { |
| 417 | int rc; |
| 418 | char *isp; |
| 419 | |
| 420 | isp = smk_of_inode(old_dentry->d_inode); |
| 421 | rc = smk_curacc(isp, MAY_WRITE); |
| 422 | |
| 423 | if (rc == 0 && new_dentry->d_inode != NULL) { |
| 424 | isp = smk_of_inode(new_dentry->d_inode); |
| 425 | rc = smk_curacc(isp, MAY_WRITE); |
| 426 | } |
| 427 | |
| 428 | return rc; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * smack_inode_unlink - Smack check on inode deletion |
| 433 | * @dir: containing directory object |
| 434 | * @dentry: file to unlink |
| 435 | * |
| 436 | * Returns 0 if current can write the containing directory |
| 437 | * and the object, error code otherwise |
| 438 | */ |
| 439 | static int smack_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 440 | { |
| 441 | struct inode *ip = dentry->d_inode; |
| 442 | int rc; |
| 443 | |
| 444 | /* |
| 445 | * You need write access to the thing you're unlinking |
| 446 | */ |
| 447 | rc = smk_curacc(smk_of_inode(ip), MAY_WRITE); |
| 448 | if (rc == 0) |
| 449 | /* |
| 450 | * You also need write access to the containing directory |
| 451 | */ |
| 452 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE); |
| 453 | |
| 454 | return rc; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * smack_inode_rmdir - Smack check on directory deletion |
| 459 | * @dir: containing directory object |
| 460 | * @dentry: directory to unlink |
| 461 | * |
| 462 | * Returns 0 if current can write the containing directory |
| 463 | * and the directory, error code otherwise |
| 464 | */ |
| 465 | static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 466 | { |
| 467 | int rc; |
| 468 | |
| 469 | /* |
| 470 | * You need write access to the thing you're removing |
| 471 | */ |
| 472 | rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); |
| 473 | if (rc == 0) |
| 474 | /* |
| 475 | * You also need write access to the containing directory |
| 476 | */ |
| 477 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE); |
| 478 | |
| 479 | return rc; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * smack_inode_rename - Smack check on rename |
| 484 | * @old_inode: the old directory |
| 485 | * @old_dentry: unused |
| 486 | * @new_inode: the new directory |
| 487 | * @new_dentry: unused |
| 488 | * |
| 489 | * Read and write access is required on both the old and |
| 490 | * new directories. |
| 491 | * |
| 492 | * Returns 0 if access is permitted, an error code otherwise |
| 493 | */ |
| 494 | static int smack_inode_rename(struct inode *old_inode, |
| 495 | struct dentry *old_dentry, |
| 496 | struct inode *new_inode, |
| 497 | struct dentry *new_dentry) |
| 498 | { |
| 499 | int rc; |
| 500 | char *isp; |
| 501 | |
| 502 | isp = smk_of_inode(old_dentry->d_inode); |
| 503 | rc = smk_curacc(isp, MAY_READWRITE); |
| 504 | |
| 505 | if (rc == 0 && new_dentry->d_inode != NULL) { |
| 506 | isp = smk_of_inode(new_dentry->d_inode); |
| 507 | rc = smk_curacc(isp, MAY_READWRITE); |
| 508 | } |
| 509 | |
| 510 | return rc; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * smack_inode_permission - Smack version of permission() |
| 515 | * @inode: the inode in question |
| 516 | * @mask: the access requested |
| 517 | * @nd: unused |
| 518 | * |
| 519 | * This is the important Smack hook. |
| 520 | * |
| 521 | * Returns 0 if access is permitted, -EACCES otherwise |
| 522 | */ |
| 523 | static int smack_inode_permission(struct inode *inode, int mask, |
| 524 | struct nameidata *nd) |
| 525 | { |
| 526 | /* |
| 527 | * No permission to check. Existence test. Yup, it's there. |
| 528 | */ |
| 529 | if (mask == 0) |
| 530 | return 0; |
| 531 | |
| 532 | return smk_curacc(smk_of_inode(inode), mask); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * smack_inode_setattr - Smack check for setting attributes |
| 537 | * @dentry: the object |
| 538 | * @iattr: for the force flag |
| 539 | * |
| 540 | * Returns 0 if access is permitted, an error code otherwise |
| 541 | */ |
| 542 | static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 543 | { |
| 544 | /* |
| 545 | * Need to allow for clearing the setuid bit. |
| 546 | */ |
| 547 | if (iattr->ia_valid & ATTR_FORCE) |
| 548 | return 0; |
| 549 | |
| 550 | return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * smack_inode_getattr - Smack check for getting attributes |
| 555 | * @mnt: unused |
| 556 | * @dentry: the object |
| 557 | * |
| 558 | * Returns 0 if access is permitted, an error code otherwise |
| 559 | */ |
| 560 | static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) |
| 561 | { |
| 562 | return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * smack_inode_setxattr - Smack check for setting xattrs |
| 567 | * @dentry: the object |
| 568 | * @name: name of the attribute |
| 569 | * @value: unused |
| 570 | * @size: unused |
| 571 | * @flags: unused |
| 572 | * |
| 573 | * This protects the Smack attribute explicitly. |
| 574 | * |
| 575 | * Returns 0 if access is permitted, an error code otherwise |
| 576 | */ |
| 577 | static int smack_inode_setxattr(struct dentry *dentry, char *name, |
| 578 | void *value, size_t size, int flags) |
| 579 | { |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 580 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 581 | |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 582 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
| 583 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || |
| 584 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { |
| 585 | if (!capable(CAP_MAC_ADMIN)) |
| 586 | rc = -EPERM; |
| 587 | } else |
| 588 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 589 | |
| 590 | if (rc == 0) |
| 591 | rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); |
| 592 | |
| 593 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
| 597 | * smack_inode_post_setxattr - Apply the Smack update approved above |
| 598 | * @dentry: object |
| 599 | * @name: attribute name |
| 600 | * @value: attribute value |
| 601 | * @size: attribute size |
| 602 | * @flags: unused |
| 603 | * |
| 604 | * Set the pointer in the inode blob to the entry found |
| 605 | * in the master label list. |
| 606 | */ |
| 607 | static void smack_inode_post_setxattr(struct dentry *dentry, char *name, |
| 608 | void *value, size_t size, int flags) |
| 609 | { |
| 610 | struct inode_smack *isp; |
| 611 | char *nsp; |
| 612 | |
| 613 | /* |
| 614 | * Not SMACK |
| 615 | */ |
| 616 | if (strcmp(name, XATTR_NAME_SMACK)) |
| 617 | return; |
| 618 | |
| 619 | if (size >= SMK_LABELLEN) |
| 620 | return; |
| 621 | |
| 622 | isp = dentry->d_inode->i_security; |
| 623 | |
| 624 | /* |
| 625 | * No locking is done here. This is a pointer |
| 626 | * assignment. |
| 627 | */ |
| 628 | nsp = smk_import(value, size); |
| 629 | if (nsp != NULL) |
| 630 | isp->smk_inode = nsp; |
| 631 | else |
| 632 | isp->smk_inode = smack_known_invalid.smk_known; |
| 633 | |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * smack_inode_getxattr - Smack check on getxattr |
| 639 | * @dentry: the object |
| 640 | * @name: unused |
| 641 | * |
| 642 | * Returns 0 if access is permitted, an error code otherwise |
| 643 | */ |
| 644 | static int smack_inode_getxattr(struct dentry *dentry, char *name) |
| 645 | { |
| 646 | return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ); |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * smack_inode_removexattr - Smack check on removexattr |
| 651 | * @dentry: the object |
| 652 | * @name: name of the attribute |
| 653 | * |
| 654 | * Removing the Smack attribute requires CAP_MAC_ADMIN |
| 655 | * |
| 656 | * Returns 0 if access is permitted, an error code otherwise |
| 657 | */ |
| 658 | static int smack_inode_removexattr(struct dentry *dentry, char *name) |
| 659 | { |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 660 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 661 | |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 662 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
| 663 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || |
| 664 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { |
| 665 | if (!capable(CAP_MAC_ADMIN)) |
| 666 | rc = -EPERM; |
| 667 | } else |
| 668 | rc = cap_inode_removexattr(dentry, name); |
| 669 | |
| 670 | if (rc == 0) |
| 671 | rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); |
| 672 | |
| 673 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
| 677 | * smack_inode_getsecurity - get smack xattrs |
| 678 | * @inode: the object |
| 679 | * @name: attribute name |
| 680 | * @buffer: where to put the result |
| 681 | * @size: size of the buffer |
| 682 | * @err: unused |
| 683 | * |
| 684 | * Returns the size of the attribute or an error code |
| 685 | */ |
| 686 | static int smack_inode_getsecurity(const struct inode *inode, |
| 687 | const char *name, void **buffer, |
| 688 | bool alloc) |
| 689 | { |
| 690 | struct socket_smack *ssp; |
| 691 | struct socket *sock; |
| 692 | struct super_block *sbp; |
| 693 | struct inode *ip = (struct inode *)inode; |
| 694 | char *isp; |
| 695 | int ilen; |
| 696 | int rc = 0; |
| 697 | |
| 698 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { |
| 699 | isp = smk_of_inode(inode); |
| 700 | ilen = strlen(isp) + 1; |
| 701 | *buffer = isp; |
| 702 | return ilen; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * The rest of the Smack xattrs are only on sockets. |
| 707 | */ |
| 708 | sbp = ip->i_sb; |
| 709 | if (sbp->s_magic != SOCKFS_MAGIC) |
| 710 | return -EOPNOTSUPP; |
| 711 | |
| 712 | sock = SOCKET_I(ip); |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 713 | if (sock == NULL || sock->sk == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 714 | return -EOPNOTSUPP; |
| 715 | |
| 716 | ssp = sock->sk->sk_security; |
| 717 | |
| 718 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) |
| 719 | isp = ssp->smk_in; |
| 720 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) |
| 721 | isp = ssp->smk_out; |
| 722 | else |
| 723 | return -EOPNOTSUPP; |
| 724 | |
| 725 | ilen = strlen(isp) + 1; |
| 726 | if (rc == 0) { |
| 727 | *buffer = isp; |
| 728 | rc = ilen; |
| 729 | } |
| 730 | |
| 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | |
| 735 | /** |
| 736 | * smack_inode_listsecurity - list the Smack attributes |
| 737 | * @inode: the object |
| 738 | * @buffer: where they go |
| 739 | * @buffer_size: size of buffer |
| 740 | * |
| 741 | * Returns 0 on success, -EINVAL otherwise |
| 742 | */ |
| 743 | static int smack_inode_listsecurity(struct inode *inode, char *buffer, |
| 744 | size_t buffer_size) |
| 745 | { |
| 746 | int len = strlen(XATTR_NAME_SMACK); |
| 747 | |
| 748 | if (buffer != NULL && len <= buffer_size) { |
| 749 | memcpy(buffer, XATTR_NAME_SMACK, len); |
| 750 | return len; |
| 751 | } |
| 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * File Hooks |
| 757 | */ |
| 758 | |
| 759 | /** |
| 760 | * smack_file_permission - Smack check on file operations |
| 761 | * @file: unused |
| 762 | * @mask: unused |
| 763 | * |
| 764 | * Returns 0 |
| 765 | * |
| 766 | * Should access checks be done on each read or write? |
| 767 | * UNICOS and SELinux say yes. |
| 768 | * Trusted Solaris, Trusted Irix, and just about everyone else says no. |
| 769 | * |
| 770 | * I'll say no for now. Smack does not do the frequent |
| 771 | * label changing that SELinux does. |
| 772 | */ |
| 773 | static int smack_file_permission(struct file *file, int mask) |
| 774 | { |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * smack_file_alloc_security - assign a file security blob |
| 780 | * @file: the object |
| 781 | * |
| 782 | * The security blob for a file is a pointer to the master |
| 783 | * label list, so no allocation is done. |
| 784 | * |
| 785 | * Returns 0 |
| 786 | */ |
| 787 | static int smack_file_alloc_security(struct file *file) |
| 788 | { |
| 789 | file->f_security = current->security; |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * smack_file_free_security - clear a file security blob |
| 795 | * @file: the object |
| 796 | * |
| 797 | * The security blob for a file is a pointer to the master |
| 798 | * label list, so no memory is freed. |
| 799 | */ |
| 800 | static void smack_file_free_security(struct file *file) |
| 801 | { |
| 802 | file->f_security = NULL; |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * smack_file_ioctl - Smack check on ioctls |
| 807 | * @file: the object |
| 808 | * @cmd: what to do |
| 809 | * @arg: unused |
| 810 | * |
| 811 | * Relies heavily on the correct use of the ioctl command conventions. |
| 812 | * |
| 813 | * Returns 0 if allowed, error code otherwise |
| 814 | */ |
| 815 | static int smack_file_ioctl(struct file *file, unsigned int cmd, |
| 816 | unsigned long arg) |
| 817 | { |
| 818 | int rc = 0; |
| 819 | |
| 820 | if (_IOC_DIR(cmd) & _IOC_WRITE) |
| 821 | rc = smk_curacc(file->f_security, MAY_WRITE); |
| 822 | |
| 823 | if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) |
| 824 | rc = smk_curacc(file->f_security, MAY_READ); |
| 825 | |
| 826 | return rc; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * smack_file_lock - Smack check on file locking |
| 831 | * @file: the object |
| 832 | * @cmd unused |
| 833 | * |
| 834 | * Returns 0 if current has write access, error code otherwise |
| 835 | */ |
| 836 | static int smack_file_lock(struct file *file, unsigned int cmd) |
| 837 | { |
| 838 | return smk_curacc(file->f_security, MAY_WRITE); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * smack_file_fcntl - Smack check on fcntl |
| 843 | * @file: the object |
| 844 | * @cmd: what action to check |
| 845 | * @arg: unused |
| 846 | * |
| 847 | * Returns 0 if current has access, error code otherwise |
| 848 | */ |
| 849 | static int smack_file_fcntl(struct file *file, unsigned int cmd, |
| 850 | unsigned long arg) |
| 851 | { |
| 852 | int rc; |
| 853 | |
| 854 | switch (cmd) { |
| 855 | case F_DUPFD: |
| 856 | case F_GETFD: |
| 857 | case F_GETFL: |
| 858 | case F_GETLK: |
| 859 | case F_GETOWN: |
| 860 | case F_GETSIG: |
| 861 | rc = smk_curacc(file->f_security, MAY_READ); |
| 862 | break; |
| 863 | case F_SETFD: |
| 864 | case F_SETFL: |
| 865 | case F_SETLK: |
| 866 | case F_SETLKW: |
| 867 | case F_SETOWN: |
| 868 | case F_SETSIG: |
| 869 | rc = smk_curacc(file->f_security, MAY_WRITE); |
| 870 | break; |
| 871 | default: |
| 872 | rc = smk_curacc(file->f_security, MAY_READWRITE); |
| 873 | } |
| 874 | |
| 875 | return rc; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * smack_file_set_fowner - set the file security blob value |
| 880 | * @file: object in question |
| 881 | * |
| 882 | * Returns 0 |
| 883 | * Further research may be required on this one. |
| 884 | */ |
| 885 | static int smack_file_set_fowner(struct file *file) |
| 886 | { |
| 887 | file->f_security = current->security; |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * smack_file_send_sigiotask - Smack on sigio |
| 893 | * @tsk: The target task |
| 894 | * @fown: the object the signal come from |
| 895 | * @signum: unused |
| 896 | * |
| 897 | * Allow a privileged task to get signals even if it shouldn't |
| 898 | * |
| 899 | * Returns 0 if a subject with the object's smack could |
| 900 | * write to the task, an error code otherwise. |
| 901 | */ |
| 902 | static int smack_file_send_sigiotask(struct task_struct *tsk, |
| 903 | struct fown_struct *fown, int signum) |
| 904 | { |
| 905 | struct file *file; |
| 906 | int rc; |
| 907 | |
| 908 | /* |
| 909 | * struct fown_struct is never outside the context of a struct file |
| 910 | */ |
| 911 | file = container_of(fown, struct file, f_owner); |
| 912 | rc = smk_access(file->f_security, tsk->security, MAY_WRITE); |
| 913 | if (rc != 0 && __capable(tsk, CAP_MAC_OVERRIDE)) |
| 914 | return 0; |
| 915 | return rc; |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * smack_file_receive - Smack file receive check |
| 920 | * @file: the object |
| 921 | * |
| 922 | * Returns 0 if current has access, error code otherwise |
| 923 | */ |
| 924 | static int smack_file_receive(struct file *file) |
| 925 | { |
| 926 | int may = 0; |
| 927 | |
| 928 | /* |
| 929 | * This code relies on bitmasks. |
| 930 | */ |
| 931 | if (file->f_mode & FMODE_READ) |
| 932 | may = MAY_READ; |
| 933 | if (file->f_mode & FMODE_WRITE) |
| 934 | may |= MAY_WRITE; |
| 935 | |
| 936 | return smk_curacc(file->f_security, may); |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Task hooks |
| 941 | */ |
| 942 | |
| 943 | /** |
| 944 | * smack_task_alloc_security - "allocate" a task blob |
| 945 | * @tsk: the task in need of a blob |
| 946 | * |
| 947 | * Smack isn't using copies of blobs. Everyone |
| 948 | * points to an immutable list. No alloc required. |
| 949 | * No data copy required. |
| 950 | * |
| 951 | * Always returns 0 |
| 952 | */ |
| 953 | static int smack_task_alloc_security(struct task_struct *tsk) |
| 954 | { |
| 955 | tsk->security = current->security; |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * smack_task_free_security - "free" a task blob |
| 962 | * @task: the task with the blob |
| 963 | * |
| 964 | * Smack isn't using copies of blobs. Everyone |
| 965 | * points to an immutable list. The blobs never go away. |
| 966 | * There is no leak here. |
| 967 | */ |
| 968 | static void smack_task_free_security(struct task_struct *task) |
| 969 | { |
| 970 | task->security = NULL; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * smack_task_setpgid - Smack check on setting pgid |
| 975 | * @p: the task object |
| 976 | * @pgid: unused |
| 977 | * |
| 978 | * Return 0 if write access is permitted |
| 979 | */ |
| 980 | static int smack_task_setpgid(struct task_struct *p, pid_t pgid) |
| 981 | { |
| 982 | return smk_curacc(p->security, MAY_WRITE); |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * smack_task_getpgid - Smack access check for getpgid |
| 987 | * @p: the object task |
| 988 | * |
| 989 | * Returns 0 if current can read the object task, error code otherwise |
| 990 | */ |
| 991 | static int smack_task_getpgid(struct task_struct *p) |
| 992 | { |
| 993 | return smk_curacc(p->security, MAY_READ); |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * smack_task_getsid - Smack access check for getsid |
| 998 | * @p: the object task |
| 999 | * |
| 1000 | * Returns 0 if current can read the object task, error code otherwise |
| 1001 | */ |
| 1002 | static int smack_task_getsid(struct task_struct *p) |
| 1003 | { |
| 1004 | return smk_curacc(p->security, MAY_READ); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * smack_task_getsecid - get the secid of the task |
| 1009 | * @p: the object task |
| 1010 | * @secid: where to put the result |
| 1011 | * |
| 1012 | * Sets the secid to contain a u32 version of the smack label. |
| 1013 | */ |
| 1014 | static void smack_task_getsecid(struct task_struct *p, u32 *secid) |
| 1015 | { |
| 1016 | *secid = smack_to_secid(p->security); |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * smack_task_setnice - Smack check on setting nice |
| 1021 | * @p: the task object |
| 1022 | * @nice: unused |
| 1023 | * |
| 1024 | * Return 0 if write access is permitted |
| 1025 | */ |
| 1026 | static int smack_task_setnice(struct task_struct *p, int nice) |
| 1027 | { |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1028 | int rc; |
| 1029 | |
| 1030 | rc = cap_task_setnice(p, nice); |
| 1031 | if (rc == 0) |
| 1032 | rc = smk_curacc(p->security, MAY_WRITE); |
| 1033 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * smack_task_setioprio - Smack check on setting ioprio |
| 1038 | * @p: the task object |
| 1039 | * @ioprio: unused |
| 1040 | * |
| 1041 | * Return 0 if write access is permitted |
| 1042 | */ |
| 1043 | static int smack_task_setioprio(struct task_struct *p, int ioprio) |
| 1044 | { |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1045 | int rc; |
| 1046 | |
| 1047 | rc = cap_task_setioprio(p, ioprio); |
| 1048 | if (rc == 0) |
| 1049 | rc = smk_curacc(p->security, MAY_WRITE); |
| 1050 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * smack_task_getioprio - Smack check on reading ioprio |
| 1055 | * @p: the task object |
| 1056 | * |
| 1057 | * Return 0 if read access is permitted |
| 1058 | */ |
| 1059 | static int smack_task_getioprio(struct task_struct *p) |
| 1060 | { |
| 1061 | return smk_curacc(p->security, MAY_READ); |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * smack_task_setscheduler - Smack check on setting scheduler |
| 1066 | * @p: the task object |
| 1067 | * @policy: unused |
| 1068 | * @lp: unused |
| 1069 | * |
| 1070 | * Return 0 if read access is permitted |
| 1071 | */ |
| 1072 | static int smack_task_setscheduler(struct task_struct *p, int policy, |
| 1073 | struct sched_param *lp) |
| 1074 | { |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1075 | int rc; |
| 1076 | |
| 1077 | rc = cap_task_setscheduler(p, policy, lp); |
| 1078 | if (rc == 0) |
| 1079 | rc = smk_curacc(p->security, MAY_WRITE); |
| 1080 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * smack_task_getscheduler - Smack check on reading scheduler |
| 1085 | * @p: the task object |
| 1086 | * |
| 1087 | * Return 0 if read access is permitted |
| 1088 | */ |
| 1089 | static int smack_task_getscheduler(struct task_struct *p) |
| 1090 | { |
| 1091 | return smk_curacc(p->security, MAY_READ); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * smack_task_movememory - Smack check on moving memory |
| 1096 | * @p: the task object |
| 1097 | * |
| 1098 | * Return 0 if write access is permitted |
| 1099 | */ |
| 1100 | static int smack_task_movememory(struct task_struct *p) |
| 1101 | { |
| 1102 | return smk_curacc(p->security, MAY_WRITE); |
| 1103 | } |
| 1104 | |
| 1105 | /** |
| 1106 | * smack_task_kill - Smack check on signal delivery |
| 1107 | * @p: the task object |
| 1108 | * @info: unused |
| 1109 | * @sig: unused |
| 1110 | * @secid: identifies the smack to use in lieu of current's |
| 1111 | * |
| 1112 | * Return 0 if write access is permitted |
| 1113 | * |
| 1114 | * The secid behavior is an artifact of an SELinux hack |
| 1115 | * in the USB code. Someday it may go away. |
| 1116 | */ |
| 1117 | static int smack_task_kill(struct task_struct *p, struct siginfo *info, |
| 1118 | int sig, u32 secid) |
| 1119 | { |
| 1120 | /* |
| 1121 | * Special cases where signals really ought to go through |
| 1122 | * in spite of policy. Stephen Smalley suggests it may |
| 1123 | * make sense to change the caller so that it doesn't |
| 1124 | * bother with the LSM hook in these cases. |
| 1125 | */ |
| 1126 | if (info != SEND_SIG_NOINFO && |
| 1127 | (is_si_special(info) || SI_FROMKERNEL(info))) |
| 1128 | return 0; |
| 1129 | /* |
| 1130 | * Sending a signal requires that the sender |
| 1131 | * can write the receiver. |
| 1132 | */ |
| 1133 | if (secid == 0) |
| 1134 | return smk_curacc(p->security, MAY_WRITE); |
| 1135 | /* |
| 1136 | * If the secid isn't 0 we're dealing with some USB IO |
| 1137 | * specific behavior. This is not clean. For one thing |
| 1138 | * we can't take privilege into account. |
| 1139 | */ |
| 1140 | return smk_access(smack_from_secid(secid), p->security, MAY_WRITE); |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * smack_task_wait - Smack access check for waiting |
| 1145 | * @p: task to wait for |
| 1146 | * |
| 1147 | * Returns 0 if current can wait for p, error code otherwise |
| 1148 | */ |
| 1149 | static int smack_task_wait(struct task_struct *p) |
| 1150 | { |
| 1151 | int rc; |
| 1152 | |
| 1153 | rc = smk_access(current->security, p->security, MAY_WRITE); |
| 1154 | if (rc == 0) |
| 1155 | return 0; |
| 1156 | |
| 1157 | /* |
| 1158 | * Allow the operation to succeed if either task |
| 1159 | * has privilege to perform operations that might |
| 1160 | * account for the smack labels having gotten to |
| 1161 | * be different in the first place. |
| 1162 | * |
| 1163 | * This breaks the strict subjet/object access |
| 1164 | * control ideal, taking the object's privilege |
| 1165 | * state into account in the decision as well as |
| 1166 | * the smack value. |
| 1167 | */ |
| 1168 | if (capable(CAP_MAC_OVERRIDE) || __capable(p, CAP_MAC_OVERRIDE)) |
| 1169 | return 0; |
| 1170 | |
| 1171 | return rc; |
| 1172 | } |
| 1173 | |
| 1174 | /** |
| 1175 | * smack_task_to_inode - copy task smack into the inode blob |
| 1176 | * @p: task to copy from |
| 1177 | * inode: inode to copy to |
| 1178 | * |
| 1179 | * Sets the smack pointer in the inode security blob |
| 1180 | */ |
| 1181 | static void smack_task_to_inode(struct task_struct *p, struct inode *inode) |
| 1182 | { |
| 1183 | struct inode_smack *isp = inode->i_security; |
| 1184 | isp->smk_inode = p->security; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * Socket hooks. |
| 1189 | */ |
| 1190 | |
| 1191 | /** |
| 1192 | * smack_sk_alloc_security - Allocate a socket blob |
| 1193 | * @sk: the socket |
| 1194 | * @family: unused |
| 1195 | * @priority: memory allocation priority |
| 1196 | * |
| 1197 | * Assign Smack pointers to current |
| 1198 | * |
| 1199 | * Returns 0 on success, -ENOMEM is there's no memory |
| 1200 | */ |
| 1201 | static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags) |
| 1202 | { |
| 1203 | char *csp = current->security; |
| 1204 | struct socket_smack *ssp; |
| 1205 | |
| 1206 | ssp = kzalloc(sizeof(struct socket_smack), gfp_flags); |
| 1207 | if (ssp == NULL) |
| 1208 | return -ENOMEM; |
| 1209 | |
| 1210 | ssp->smk_in = csp; |
| 1211 | ssp->smk_out = csp; |
| 1212 | ssp->smk_packet[0] = '\0'; |
| 1213 | |
| 1214 | sk->sk_security = ssp; |
| 1215 | |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * smack_sk_free_security - Free a socket blob |
| 1221 | * @sk: the socket |
| 1222 | * |
| 1223 | * Clears the blob pointer |
| 1224 | */ |
| 1225 | static void smack_sk_free_security(struct sock *sk) |
| 1226 | { |
| 1227 | kfree(sk->sk_security); |
| 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * smack_set_catset - convert a capset to netlabel mls categories |
| 1232 | * @catset: the Smack categories |
| 1233 | * @sap: where to put the netlabel categories |
| 1234 | * |
| 1235 | * Allocates and fills attr.mls.cat |
| 1236 | */ |
| 1237 | static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap) |
| 1238 | { |
| 1239 | unsigned char *cp; |
| 1240 | unsigned char m; |
| 1241 | int cat; |
| 1242 | int rc; |
| 1243 | int byte; |
| 1244 | |
Harvey Harrison | c60264c | 2008-04-28 02:13:41 -0700 | [diff] [blame] | 1245 | if (!catset) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1246 | return; |
| 1247 | |
| 1248 | sap->flags |= NETLBL_SECATTR_MLS_CAT; |
| 1249 | sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC); |
| 1250 | sap->attr.mls.cat->startbit = 0; |
| 1251 | |
| 1252 | for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++) |
| 1253 | for (m = 0x80; m != 0; m >>= 1, cat++) { |
| 1254 | if ((m & *cp) == 0) |
| 1255 | continue; |
| 1256 | rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat, |
| 1257 | cat, GFP_ATOMIC); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * smack_to_secattr - fill a secattr from a smack value |
| 1263 | * @smack: the smack value |
| 1264 | * @nlsp: where the result goes |
| 1265 | * |
| 1266 | * Casey says that CIPSO is good enough for now. |
| 1267 | * It can be used to effect. |
| 1268 | * It can also be abused to effect when necessary. |
| 1269 | * Appologies to the TSIG group in general and GW in particular. |
| 1270 | */ |
| 1271 | static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp) |
| 1272 | { |
| 1273 | struct smack_cipso cipso; |
| 1274 | int rc; |
| 1275 | |
| 1276 | switch (smack_net_nltype) { |
| 1277 | case NETLBL_NLTYPE_CIPSOV4: |
Paul Moore | 0044787 | 2008-04-12 19:06:42 -0700 | [diff] [blame] | 1278 | nlsp->domain = smack; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1279 | nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1280 | |
| 1281 | rc = smack_to_cipso(smack, &cipso); |
| 1282 | if (rc == 0) { |
| 1283 | nlsp->attr.mls.lvl = cipso.smk_level; |
| 1284 | smack_set_catset(cipso.smk_catset, nlsp); |
| 1285 | } else { |
| 1286 | nlsp->attr.mls.lvl = smack_cipso_direct; |
| 1287 | smack_set_catset(smack, nlsp); |
| 1288 | } |
| 1289 | break; |
| 1290 | default: |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * smack_netlabel - Set the secattr on a socket |
| 1297 | * @sk: the socket |
| 1298 | * |
| 1299 | * Convert the outbound smack value (smk_out) to a |
| 1300 | * secattr and attach it to the socket. |
| 1301 | * |
| 1302 | * Returns 0 on success or an error code |
| 1303 | */ |
| 1304 | static int smack_netlabel(struct sock *sk) |
| 1305 | { |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 1306 | struct socket_smack *ssp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1307 | struct netlbl_lsm_secattr secattr; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1308 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1309 | |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 1310 | ssp = sk->sk_security; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1311 | netlbl_secattr_init(&secattr); |
| 1312 | smack_to_secattr(ssp->smk_out, &secattr); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1313 | rc = netlbl_sock_setattr(sk, &secattr); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1314 | netlbl_secattr_destroy(&secattr); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1315 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1316 | return rc; |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * smack_inode_setsecurity - set smack xattrs |
| 1321 | * @inode: the object |
| 1322 | * @name: attribute name |
| 1323 | * @value: attribute value |
| 1324 | * @size: size of the attribute |
| 1325 | * @flags: unused |
| 1326 | * |
| 1327 | * Sets the named attribute in the appropriate blob |
| 1328 | * |
| 1329 | * Returns 0 on success, or an error code |
| 1330 | */ |
| 1331 | static int smack_inode_setsecurity(struct inode *inode, const char *name, |
| 1332 | const void *value, size_t size, int flags) |
| 1333 | { |
| 1334 | char *sp; |
| 1335 | struct inode_smack *nsp = inode->i_security; |
| 1336 | struct socket_smack *ssp; |
| 1337 | struct socket *sock; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1338 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1339 | |
| 1340 | if (value == NULL || size > SMK_LABELLEN) |
| 1341 | return -EACCES; |
| 1342 | |
| 1343 | sp = smk_import(value, size); |
| 1344 | if (sp == NULL) |
| 1345 | return -EINVAL; |
| 1346 | |
| 1347 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { |
| 1348 | nsp->smk_inode = sp; |
| 1349 | return 0; |
| 1350 | } |
| 1351 | /* |
| 1352 | * The rest of the Smack xattrs are only on sockets. |
| 1353 | */ |
| 1354 | if (inode->i_sb->s_magic != SOCKFS_MAGIC) |
| 1355 | return -EOPNOTSUPP; |
| 1356 | |
| 1357 | sock = SOCKET_I(inode); |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 1358 | if (sock == NULL || sock->sk == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1359 | return -EOPNOTSUPP; |
| 1360 | |
| 1361 | ssp = sock->sk->sk_security; |
| 1362 | |
| 1363 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) |
| 1364 | ssp->smk_in = sp; |
| 1365 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) { |
| 1366 | ssp->smk_out = sp; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1367 | rc = smack_netlabel(sock->sk); |
| 1368 | if (rc != 0) |
| 1369 | printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n", |
| 1370 | __func__, -rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1371 | } else |
| 1372 | return -EOPNOTSUPP; |
| 1373 | |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * smack_socket_post_create - finish socket setup |
| 1379 | * @sock: the socket |
| 1380 | * @family: protocol family |
| 1381 | * @type: unused |
| 1382 | * @protocol: unused |
| 1383 | * @kern: unused |
| 1384 | * |
| 1385 | * Sets the netlabel information on the socket |
| 1386 | * |
| 1387 | * Returns 0 on success, and error code otherwise |
| 1388 | */ |
| 1389 | static int smack_socket_post_create(struct socket *sock, int family, |
| 1390 | int type, int protocol, int kern) |
| 1391 | { |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 1392 | if (family != PF_INET || sock->sk == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1393 | return 0; |
| 1394 | /* |
| 1395 | * Set the outbound netlbl. |
| 1396 | */ |
| 1397 | return smack_netlabel(sock->sk); |
| 1398 | } |
| 1399 | |
| 1400 | /** |
| 1401 | * smack_flags_to_may - convert S_ to MAY_ values |
| 1402 | * @flags: the S_ value |
| 1403 | * |
| 1404 | * Returns the equivalent MAY_ value |
| 1405 | */ |
| 1406 | static int smack_flags_to_may(int flags) |
| 1407 | { |
| 1408 | int may = 0; |
| 1409 | |
| 1410 | if (flags & S_IRUGO) |
| 1411 | may |= MAY_READ; |
| 1412 | if (flags & S_IWUGO) |
| 1413 | may |= MAY_WRITE; |
| 1414 | if (flags & S_IXUGO) |
| 1415 | may |= MAY_EXEC; |
| 1416 | |
| 1417 | return may; |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * smack_msg_msg_alloc_security - Set the security blob for msg_msg |
| 1422 | * @msg: the object |
| 1423 | * |
| 1424 | * Returns 0 |
| 1425 | */ |
| 1426 | static int smack_msg_msg_alloc_security(struct msg_msg *msg) |
| 1427 | { |
| 1428 | msg->security = current->security; |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * smack_msg_msg_free_security - Clear the security blob for msg_msg |
| 1434 | * @msg: the object |
| 1435 | * |
| 1436 | * Clears the blob pointer |
| 1437 | */ |
| 1438 | static void smack_msg_msg_free_security(struct msg_msg *msg) |
| 1439 | { |
| 1440 | msg->security = NULL; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * smack_of_shm - the smack pointer for the shm |
| 1445 | * @shp: the object |
| 1446 | * |
| 1447 | * Returns a pointer to the smack value |
| 1448 | */ |
| 1449 | static char *smack_of_shm(struct shmid_kernel *shp) |
| 1450 | { |
| 1451 | return (char *)shp->shm_perm.security; |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * smack_shm_alloc_security - Set the security blob for shm |
| 1456 | * @shp: the object |
| 1457 | * |
| 1458 | * Returns 0 |
| 1459 | */ |
| 1460 | static int smack_shm_alloc_security(struct shmid_kernel *shp) |
| 1461 | { |
| 1462 | struct kern_ipc_perm *isp = &shp->shm_perm; |
| 1463 | |
| 1464 | isp->security = current->security; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | /** |
| 1469 | * smack_shm_free_security - Clear the security blob for shm |
| 1470 | * @shp: the object |
| 1471 | * |
| 1472 | * Clears the blob pointer |
| 1473 | */ |
| 1474 | static void smack_shm_free_security(struct shmid_kernel *shp) |
| 1475 | { |
| 1476 | struct kern_ipc_perm *isp = &shp->shm_perm; |
| 1477 | |
| 1478 | isp->security = NULL; |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * smack_shm_associate - Smack access check for shm |
| 1483 | * @shp: the object |
| 1484 | * @shmflg: access requested |
| 1485 | * |
| 1486 | * Returns 0 if current has the requested access, error code otherwise |
| 1487 | */ |
| 1488 | static int smack_shm_associate(struct shmid_kernel *shp, int shmflg) |
| 1489 | { |
| 1490 | char *ssp = smack_of_shm(shp); |
| 1491 | int may; |
| 1492 | |
| 1493 | may = smack_flags_to_may(shmflg); |
| 1494 | return smk_curacc(ssp, may); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * smack_shm_shmctl - Smack access check for shm |
| 1499 | * @shp: the object |
| 1500 | * @cmd: what it wants to do |
| 1501 | * |
| 1502 | * Returns 0 if current has the requested access, error code otherwise |
| 1503 | */ |
| 1504 | static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd) |
| 1505 | { |
Ahmed S. Darwish | 1d252fb | 2008-03-19 17:00:51 -0700 | [diff] [blame] | 1506 | char *ssp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1507 | int may; |
| 1508 | |
| 1509 | switch (cmd) { |
| 1510 | case IPC_STAT: |
| 1511 | case SHM_STAT: |
| 1512 | may = MAY_READ; |
| 1513 | break; |
| 1514 | case IPC_SET: |
| 1515 | case SHM_LOCK: |
| 1516 | case SHM_UNLOCK: |
| 1517 | case IPC_RMID: |
| 1518 | may = MAY_READWRITE; |
| 1519 | break; |
| 1520 | case IPC_INFO: |
| 1521 | case SHM_INFO: |
| 1522 | /* |
| 1523 | * System level information. |
| 1524 | */ |
| 1525 | return 0; |
| 1526 | default: |
| 1527 | return -EINVAL; |
| 1528 | } |
| 1529 | |
Ahmed S. Darwish | 1d252fb | 2008-03-19 17:00:51 -0700 | [diff] [blame] | 1530 | ssp = smack_of_shm(shp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1531 | return smk_curacc(ssp, may); |
| 1532 | } |
| 1533 | |
| 1534 | /** |
| 1535 | * smack_shm_shmat - Smack access for shmat |
| 1536 | * @shp: the object |
| 1537 | * @shmaddr: unused |
| 1538 | * @shmflg: access requested |
| 1539 | * |
| 1540 | * Returns 0 if current has the requested access, error code otherwise |
| 1541 | */ |
| 1542 | static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, |
| 1543 | int shmflg) |
| 1544 | { |
| 1545 | char *ssp = smack_of_shm(shp); |
| 1546 | int may; |
| 1547 | |
| 1548 | may = smack_flags_to_may(shmflg); |
| 1549 | return smk_curacc(ssp, may); |
| 1550 | } |
| 1551 | |
| 1552 | /** |
| 1553 | * smack_of_sem - the smack pointer for the sem |
| 1554 | * @sma: the object |
| 1555 | * |
| 1556 | * Returns a pointer to the smack value |
| 1557 | */ |
| 1558 | static char *smack_of_sem(struct sem_array *sma) |
| 1559 | { |
| 1560 | return (char *)sma->sem_perm.security; |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * smack_sem_alloc_security - Set the security blob for sem |
| 1565 | * @sma: the object |
| 1566 | * |
| 1567 | * Returns 0 |
| 1568 | */ |
| 1569 | static int smack_sem_alloc_security(struct sem_array *sma) |
| 1570 | { |
| 1571 | struct kern_ipc_perm *isp = &sma->sem_perm; |
| 1572 | |
| 1573 | isp->security = current->security; |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * smack_sem_free_security - Clear the security blob for sem |
| 1579 | * @sma: the object |
| 1580 | * |
| 1581 | * Clears the blob pointer |
| 1582 | */ |
| 1583 | static void smack_sem_free_security(struct sem_array *sma) |
| 1584 | { |
| 1585 | struct kern_ipc_perm *isp = &sma->sem_perm; |
| 1586 | |
| 1587 | isp->security = NULL; |
| 1588 | } |
| 1589 | |
| 1590 | /** |
| 1591 | * smack_sem_associate - Smack access check for sem |
| 1592 | * @sma: the object |
| 1593 | * @semflg: access requested |
| 1594 | * |
| 1595 | * Returns 0 if current has the requested access, error code otherwise |
| 1596 | */ |
| 1597 | static int smack_sem_associate(struct sem_array *sma, int semflg) |
| 1598 | { |
| 1599 | char *ssp = smack_of_sem(sma); |
| 1600 | int may; |
| 1601 | |
| 1602 | may = smack_flags_to_may(semflg); |
| 1603 | return smk_curacc(ssp, may); |
| 1604 | } |
| 1605 | |
| 1606 | /** |
| 1607 | * smack_sem_shmctl - Smack access check for sem |
| 1608 | * @sma: the object |
| 1609 | * @cmd: what it wants to do |
| 1610 | * |
| 1611 | * Returns 0 if current has the requested access, error code otherwise |
| 1612 | */ |
| 1613 | static int smack_sem_semctl(struct sem_array *sma, int cmd) |
| 1614 | { |
Ahmed S. Darwish | 1d252fb | 2008-03-19 17:00:51 -0700 | [diff] [blame] | 1615 | char *ssp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1616 | int may; |
| 1617 | |
| 1618 | switch (cmd) { |
| 1619 | case GETPID: |
| 1620 | case GETNCNT: |
| 1621 | case GETZCNT: |
| 1622 | case GETVAL: |
| 1623 | case GETALL: |
| 1624 | case IPC_STAT: |
| 1625 | case SEM_STAT: |
| 1626 | may = MAY_READ; |
| 1627 | break; |
| 1628 | case SETVAL: |
| 1629 | case SETALL: |
| 1630 | case IPC_RMID: |
| 1631 | case IPC_SET: |
| 1632 | may = MAY_READWRITE; |
| 1633 | break; |
| 1634 | case IPC_INFO: |
| 1635 | case SEM_INFO: |
| 1636 | /* |
| 1637 | * System level information |
| 1638 | */ |
| 1639 | return 0; |
| 1640 | default: |
| 1641 | return -EINVAL; |
| 1642 | } |
| 1643 | |
Ahmed S. Darwish | 1d252fb | 2008-03-19 17:00:51 -0700 | [diff] [blame] | 1644 | ssp = smack_of_sem(sma); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1645 | return smk_curacc(ssp, may); |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * smack_sem_semop - Smack checks of semaphore operations |
| 1650 | * @sma: the object |
| 1651 | * @sops: unused |
| 1652 | * @nsops: unused |
| 1653 | * @alter: unused |
| 1654 | * |
| 1655 | * Treated as read and write in all cases. |
| 1656 | * |
| 1657 | * Returns 0 if access is allowed, error code otherwise |
| 1658 | */ |
| 1659 | static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops, |
| 1660 | unsigned nsops, int alter) |
| 1661 | { |
| 1662 | char *ssp = smack_of_sem(sma); |
| 1663 | |
| 1664 | return smk_curacc(ssp, MAY_READWRITE); |
| 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * smack_msg_alloc_security - Set the security blob for msg |
| 1669 | * @msq: the object |
| 1670 | * |
| 1671 | * Returns 0 |
| 1672 | */ |
| 1673 | static int smack_msg_queue_alloc_security(struct msg_queue *msq) |
| 1674 | { |
| 1675 | struct kern_ipc_perm *kisp = &msq->q_perm; |
| 1676 | |
| 1677 | kisp->security = current->security; |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | /** |
| 1682 | * smack_msg_free_security - Clear the security blob for msg |
| 1683 | * @msq: the object |
| 1684 | * |
| 1685 | * Clears the blob pointer |
| 1686 | */ |
| 1687 | static void smack_msg_queue_free_security(struct msg_queue *msq) |
| 1688 | { |
| 1689 | struct kern_ipc_perm *kisp = &msq->q_perm; |
| 1690 | |
| 1691 | kisp->security = NULL; |
| 1692 | } |
| 1693 | |
| 1694 | /** |
| 1695 | * smack_of_msq - the smack pointer for the msq |
| 1696 | * @msq: the object |
| 1697 | * |
| 1698 | * Returns a pointer to the smack value |
| 1699 | */ |
| 1700 | static char *smack_of_msq(struct msg_queue *msq) |
| 1701 | { |
| 1702 | return (char *)msq->q_perm.security; |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * smack_msg_queue_associate - Smack access check for msg_queue |
| 1707 | * @msq: the object |
| 1708 | * @msqflg: access requested |
| 1709 | * |
| 1710 | * Returns 0 if current has the requested access, error code otherwise |
| 1711 | */ |
| 1712 | static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg) |
| 1713 | { |
| 1714 | char *msp = smack_of_msq(msq); |
| 1715 | int may; |
| 1716 | |
| 1717 | may = smack_flags_to_may(msqflg); |
| 1718 | return smk_curacc(msp, may); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * smack_msg_queue_msgctl - Smack access check for msg_queue |
| 1723 | * @msq: the object |
| 1724 | * @cmd: what it wants to do |
| 1725 | * |
| 1726 | * Returns 0 if current has the requested access, error code otherwise |
| 1727 | */ |
| 1728 | static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd) |
| 1729 | { |
Ahmed S. Darwish | 1d252fb | 2008-03-19 17:00:51 -0700 | [diff] [blame] | 1730 | char *msp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1731 | int may; |
| 1732 | |
| 1733 | switch (cmd) { |
| 1734 | case IPC_STAT: |
| 1735 | case MSG_STAT: |
| 1736 | may = MAY_READ; |
| 1737 | break; |
| 1738 | case IPC_SET: |
| 1739 | case IPC_RMID: |
| 1740 | may = MAY_READWRITE; |
| 1741 | break; |
| 1742 | case IPC_INFO: |
| 1743 | case MSG_INFO: |
| 1744 | /* |
| 1745 | * System level information |
| 1746 | */ |
| 1747 | return 0; |
| 1748 | default: |
| 1749 | return -EINVAL; |
| 1750 | } |
| 1751 | |
Ahmed S. Darwish | 1d252fb | 2008-03-19 17:00:51 -0700 | [diff] [blame] | 1752 | msp = smack_of_msq(msq); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1753 | return smk_curacc(msp, may); |
| 1754 | } |
| 1755 | |
| 1756 | /** |
| 1757 | * smack_msg_queue_msgsnd - Smack access check for msg_queue |
| 1758 | * @msq: the object |
| 1759 | * @msg: unused |
| 1760 | * @msqflg: access requested |
| 1761 | * |
| 1762 | * Returns 0 if current has the requested access, error code otherwise |
| 1763 | */ |
| 1764 | static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, |
| 1765 | int msqflg) |
| 1766 | { |
| 1767 | char *msp = smack_of_msq(msq); |
| 1768 | int rc; |
| 1769 | |
| 1770 | rc = smack_flags_to_may(msqflg); |
| 1771 | return smk_curacc(msp, rc); |
| 1772 | } |
| 1773 | |
| 1774 | /** |
| 1775 | * smack_msg_queue_msgsnd - Smack access check for msg_queue |
| 1776 | * @msq: the object |
| 1777 | * @msg: unused |
| 1778 | * @target: unused |
| 1779 | * @type: unused |
| 1780 | * @mode: unused |
| 1781 | * |
| 1782 | * Returns 0 if current has read and write access, error code otherwise |
| 1783 | */ |
| 1784 | static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, |
| 1785 | struct task_struct *target, long type, int mode) |
| 1786 | { |
| 1787 | char *msp = smack_of_msq(msq); |
| 1788 | |
| 1789 | return smk_curacc(msp, MAY_READWRITE); |
| 1790 | } |
| 1791 | |
| 1792 | /** |
| 1793 | * smack_ipc_permission - Smack access for ipc_permission() |
| 1794 | * @ipp: the object permissions |
| 1795 | * @flag: access requested |
| 1796 | * |
| 1797 | * Returns 0 if current has read and write access, error code otherwise |
| 1798 | */ |
| 1799 | static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) |
| 1800 | { |
| 1801 | char *isp = ipp->security; |
| 1802 | int may; |
| 1803 | |
| 1804 | may = smack_flags_to_may(flag); |
| 1805 | return smk_curacc(isp, may); |
| 1806 | } |
| 1807 | |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1808 | /* module stacking operations */ |
| 1809 | |
| 1810 | /** |
| 1811 | * smack_register_security - stack capability module |
| 1812 | * @name: module name |
| 1813 | * @ops: module operations - ignored |
| 1814 | * |
| 1815 | * Allow the capability module to register. |
| 1816 | */ |
| 1817 | static int smack_register_security(const char *name, |
| 1818 | struct security_operations *ops) |
| 1819 | { |
| 1820 | if (strcmp(name, "capability") != 0) |
| 1821 | return -EINVAL; |
| 1822 | |
| 1823 | printk(KERN_INFO "%s: Registering secondary module %s\n", |
| 1824 | __func__, name); |
| 1825 | |
| 1826 | return 0; |
| 1827 | } |
| 1828 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1829 | /** |
| 1830 | * smack_d_instantiate - Make sure the blob is correct on an inode |
| 1831 | * @opt_dentry: unused |
| 1832 | * @inode: the object |
| 1833 | * |
| 1834 | * Set the inode's security blob if it hasn't been done already. |
| 1835 | */ |
| 1836 | static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) |
| 1837 | { |
| 1838 | struct super_block *sbp; |
| 1839 | struct superblock_smack *sbsp; |
| 1840 | struct inode_smack *isp; |
| 1841 | char *csp = current->security; |
| 1842 | char *fetched; |
| 1843 | char *final; |
| 1844 | struct dentry *dp; |
| 1845 | |
| 1846 | if (inode == NULL) |
| 1847 | return; |
| 1848 | |
| 1849 | isp = inode->i_security; |
| 1850 | |
| 1851 | mutex_lock(&isp->smk_lock); |
| 1852 | /* |
| 1853 | * If the inode is already instantiated |
| 1854 | * take the quick way out |
| 1855 | */ |
| 1856 | if (isp->smk_flags & SMK_INODE_INSTANT) |
| 1857 | goto unlockandout; |
| 1858 | |
| 1859 | sbp = inode->i_sb; |
| 1860 | sbsp = sbp->s_security; |
| 1861 | /* |
| 1862 | * We're going to use the superblock default label |
| 1863 | * if there's no label on the file. |
| 1864 | */ |
| 1865 | final = sbsp->smk_default; |
| 1866 | |
| 1867 | /* |
| 1868 | * This is pretty hackish. |
| 1869 | * Casey says that we shouldn't have to do |
| 1870 | * file system specific code, but it does help |
| 1871 | * with keeping it simple. |
| 1872 | */ |
| 1873 | switch (sbp->s_magic) { |
| 1874 | case SMACK_MAGIC: |
| 1875 | /* |
| 1876 | * Casey says that it's a little embarassing |
| 1877 | * that the smack file system doesn't do |
| 1878 | * extended attributes. |
| 1879 | */ |
| 1880 | final = smack_known_star.smk_known; |
| 1881 | break; |
| 1882 | case PIPEFS_MAGIC: |
| 1883 | /* |
| 1884 | * Casey says pipes are easy (?) |
| 1885 | */ |
| 1886 | final = smack_known_star.smk_known; |
| 1887 | break; |
| 1888 | case DEVPTS_SUPER_MAGIC: |
| 1889 | /* |
| 1890 | * devpts seems content with the label of the task. |
| 1891 | * Programs that change smack have to treat the |
| 1892 | * pty with respect. |
| 1893 | */ |
| 1894 | final = csp; |
| 1895 | break; |
| 1896 | case SOCKFS_MAGIC: |
| 1897 | /* |
| 1898 | * Casey says sockets get the smack of the task. |
| 1899 | */ |
| 1900 | final = csp; |
| 1901 | break; |
| 1902 | case PROC_SUPER_MAGIC: |
| 1903 | /* |
| 1904 | * Casey says procfs appears not to care. |
| 1905 | * The superblock default suffices. |
| 1906 | */ |
| 1907 | break; |
| 1908 | case TMPFS_MAGIC: |
| 1909 | /* |
| 1910 | * Device labels should come from the filesystem, |
| 1911 | * but watch out, because they're volitile, |
| 1912 | * getting recreated on every reboot. |
| 1913 | */ |
| 1914 | final = smack_known_star.smk_known; |
| 1915 | /* |
| 1916 | * No break. |
| 1917 | * |
| 1918 | * If a smack value has been set we want to use it, |
| 1919 | * but since tmpfs isn't giving us the opportunity |
| 1920 | * to set mount options simulate setting the |
| 1921 | * superblock default. |
| 1922 | */ |
| 1923 | default: |
| 1924 | /* |
| 1925 | * This isn't an understood special case. |
| 1926 | * Get the value from the xattr. |
| 1927 | * |
| 1928 | * No xattr support means, alas, no SMACK label. |
| 1929 | * Use the aforeapplied default. |
| 1930 | * It would be curious if the label of the task |
| 1931 | * does not match that assigned. |
| 1932 | */ |
| 1933 | if (inode->i_op->getxattr == NULL) |
| 1934 | break; |
| 1935 | /* |
| 1936 | * Get the dentry for xattr. |
| 1937 | */ |
| 1938 | if (opt_dentry == NULL) { |
| 1939 | dp = d_find_alias(inode); |
| 1940 | if (dp == NULL) |
| 1941 | break; |
| 1942 | } else { |
| 1943 | dp = dget(opt_dentry); |
| 1944 | if (dp == NULL) |
| 1945 | break; |
| 1946 | } |
| 1947 | |
| 1948 | fetched = smk_fetch(inode, dp); |
| 1949 | if (fetched != NULL) |
| 1950 | final = fetched; |
| 1951 | |
| 1952 | dput(dp); |
| 1953 | break; |
| 1954 | } |
| 1955 | |
| 1956 | if (final == NULL) |
| 1957 | isp->smk_inode = csp; |
| 1958 | else |
| 1959 | isp->smk_inode = final; |
| 1960 | |
| 1961 | isp->smk_flags |= SMK_INODE_INSTANT; |
| 1962 | |
| 1963 | unlockandout: |
| 1964 | mutex_unlock(&isp->smk_lock); |
| 1965 | return; |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * smack_getprocattr - Smack process attribute access |
| 1970 | * @p: the object task |
| 1971 | * @name: the name of the attribute in /proc/.../attr |
| 1972 | * @value: where to put the result |
| 1973 | * |
| 1974 | * Places a copy of the task Smack into value |
| 1975 | * |
| 1976 | * Returns the length of the smack label or an error code |
| 1977 | */ |
| 1978 | static int smack_getprocattr(struct task_struct *p, char *name, char **value) |
| 1979 | { |
| 1980 | char *cp; |
| 1981 | int slen; |
| 1982 | |
| 1983 | if (strcmp(name, "current") != 0) |
| 1984 | return -EINVAL; |
| 1985 | |
| 1986 | cp = kstrdup(p->security, GFP_KERNEL); |
| 1987 | if (cp == NULL) |
| 1988 | return -ENOMEM; |
| 1989 | |
| 1990 | slen = strlen(cp); |
| 1991 | *value = cp; |
| 1992 | return slen; |
| 1993 | } |
| 1994 | |
| 1995 | /** |
| 1996 | * smack_setprocattr - Smack process attribute setting |
| 1997 | * @p: the object task |
| 1998 | * @name: the name of the attribute in /proc/.../attr |
| 1999 | * @value: the value to set |
| 2000 | * @size: the size of the value |
| 2001 | * |
| 2002 | * Sets the Smack value of the task. Only setting self |
| 2003 | * is permitted and only with privilege |
| 2004 | * |
| 2005 | * Returns the length of the smack label or an error code |
| 2006 | */ |
| 2007 | static int smack_setprocattr(struct task_struct *p, char *name, |
| 2008 | void *value, size_t size) |
| 2009 | { |
| 2010 | char *newsmack; |
| 2011 | |
| 2012 | if (!__capable(p, CAP_MAC_ADMIN)) |
| 2013 | return -EPERM; |
| 2014 | |
| 2015 | /* |
| 2016 | * Changing another process' Smack value is too dangerous |
| 2017 | * and supports no sane use case. |
| 2018 | */ |
| 2019 | if (p != current) |
| 2020 | return -EPERM; |
| 2021 | |
| 2022 | if (value == NULL || size == 0 || size >= SMK_LABELLEN) |
| 2023 | return -EINVAL; |
| 2024 | |
| 2025 | if (strcmp(name, "current") != 0) |
| 2026 | return -EINVAL; |
| 2027 | |
| 2028 | newsmack = smk_import(value, size); |
| 2029 | if (newsmack == NULL) |
| 2030 | return -EINVAL; |
| 2031 | |
| 2032 | p->security = newsmack; |
| 2033 | return size; |
| 2034 | } |
| 2035 | |
| 2036 | /** |
| 2037 | * smack_unix_stream_connect - Smack access on UDS |
| 2038 | * @sock: one socket |
| 2039 | * @other: the other socket |
| 2040 | * @newsk: unused |
| 2041 | * |
| 2042 | * Return 0 if a subject with the smack of sock could access |
| 2043 | * an object with the smack of other, otherwise an error code |
| 2044 | */ |
| 2045 | static int smack_unix_stream_connect(struct socket *sock, |
| 2046 | struct socket *other, struct sock *newsk) |
| 2047 | { |
| 2048 | struct inode *sp = SOCK_INODE(sock); |
| 2049 | struct inode *op = SOCK_INODE(other); |
| 2050 | |
| 2051 | return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE); |
| 2052 | } |
| 2053 | |
| 2054 | /** |
| 2055 | * smack_unix_may_send - Smack access on UDS |
| 2056 | * @sock: one socket |
| 2057 | * @other: the other socket |
| 2058 | * |
| 2059 | * Return 0 if a subject with the smack of sock could access |
| 2060 | * an object with the smack of other, otherwise an error code |
| 2061 | */ |
| 2062 | static int smack_unix_may_send(struct socket *sock, struct socket *other) |
| 2063 | { |
| 2064 | struct inode *sp = SOCK_INODE(sock); |
| 2065 | struct inode *op = SOCK_INODE(other); |
| 2066 | |
| 2067 | return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE); |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat |
| 2072 | * pair to smack |
| 2073 | * @sap: netlabel secattr |
| 2074 | * @sip: where to put the result |
| 2075 | * |
| 2076 | * Copies a smack label into sip |
| 2077 | */ |
| 2078 | static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip) |
| 2079 | { |
| 2080 | char smack[SMK_LABELLEN]; |
| 2081 | int pcat; |
| 2082 | |
| 2083 | if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) { |
| 2084 | /* |
| 2085 | * If there are flags but no level netlabel isn't |
| 2086 | * behaving the way we expect it to. |
| 2087 | * |
| 2088 | * Without guidance regarding the smack value |
| 2089 | * for the packet fall back on the network |
| 2090 | * ambient value. |
| 2091 | */ |
| 2092 | strncpy(sip, smack_net_ambient, SMK_MAXLEN); |
| 2093 | return; |
| 2094 | } |
| 2095 | /* |
| 2096 | * Get the categories, if any |
| 2097 | */ |
| 2098 | memset(smack, '\0', SMK_LABELLEN); |
| 2099 | if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0) |
| 2100 | for (pcat = -1;;) { |
| 2101 | pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat, |
| 2102 | pcat + 1); |
| 2103 | if (pcat < 0) |
| 2104 | break; |
| 2105 | smack_catset_bit(pcat, smack); |
| 2106 | } |
| 2107 | /* |
| 2108 | * If it is CIPSO using smack direct mapping |
| 2109 | * we are already done. WeeHee. |
| 2110 | */ |
| 2111 | if (sap->attr.mls.lvl == smack_cipso_direct) { |
| 2112 | memcpy(sip, smack, SMK_MAXLEN); |
| 2113 | return; |
| 2114 | } |
| 2115 | /* |
| 2116 | * Look it up in the supplied table if it is not a direct mapping. |
| 2117 | */ |
| 2118 | smack_from_cipso(sap->attr.mls.lvl, smack, sip); |
| 2119 | return; |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * smack_socket_sock_rcv_skb - Smack packet delivery access check |
| 2124 | * @sk: socket |
| 2125 | * @skb: packet |
| 2126 | * |
| 2127 | * Returns 0 if the packet should be delivered, an error code otherwise |
| 2128 | */ |
| 2129 | static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 2130 | { |
| 2131 | struct netlbl_lsm_secattr secattr; |
| 2132 | struct socket_smack *ssp = sk->sk_security; |
| 2133 | char smack[SMK_LABELLEN]; |
| 2134 | int rc; |
| 2135 | |
| 2136 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) |
| 2137 | return 0; |
| 2138 | |
| 2139 | /* |
| 2140 | * Translate what netlabel gave us. |
| 2141 | */ |
| 2142 | memset(smack, '\0', SMK_LABELLEN); |
| 2143 | netlbl_secattr_init(&secattr); |
| 2144 | rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr); |
| 2145 | if (rc == 0) |
| 2146 | smack_from_secattr(&secattr, smack); |
| 2147 | else |
| 2148 | strncpy(smack, smack_net_ambient, SMK_MAXLEN); |
| 2149 | netlbl_secattr_destroy(&secattr); |
| 2150 | /* |
| 2151 | * Receiving a packet requires that the other end |
| 2152 | * be able to write here. Read access is not required. |
| 2153 | * This is the simplist possible security model |
| 2154 | * for networking. |
| 2155 | */ |
| 2156 | return smk_access(smack, ssp->smk_in, MAY_WRITE); |
| 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * smack_socket_getpeersec_stream - pull in packet label |
| 2161 | * @sock: the socket |
| 2162 | * @optval: user's destination |
| 2163 | * @optlen: size thereof |
| 2164 | * @len: max thereoe |
| 2165 | * |
| 2166 | * returns zero on success, an error code otherwise |
| 2167 | */ |
| 2168 | static int smack_socket_getpeersec_stream(struct socket *sock, |
| 2169 | char __user *optval, |
| 2170 | int __user *optlen, unsigned len) |
| 2171 | { |
| 2172 | struct socket_smack *ssp; |
| 2173 | int slen; |
| 2174 | int rc = 0; |
| 2175 | |
| 2176 | ssp = sock->sk->sk_security; |
| 2177 | slen = strlen(ssp->smk_packet) + 1; |
| 2178 | |
| 2179 | if (slen > len) |
| 2180 | rc = -ERANGE; |
| 2181 | else if (copy_to_user(optval, ssp->smk_packet, slen) != 0) |
| 2182 | rc = -EFAULT; |
| 2183 | |
| 2184 | if (put_user(slen, optlen) != 0) |
| 2185 | rc = -EFAULT; |
| 2186 | |
| 2187 | return rc; |
| 2188 | } |
| 2189 | |
| 2190 | |
| 2191 | /** |
| 2192 | * smack_socket_getpeersec_dgram - pull in packet label |
| 2193 | * @sock: the socket |
| 2194 | * @skb: packet data |
| 2195 | * @secid: pointer to where to put the secid of the packet |
| 2196 | * |
| 2197 | * Sets the netlabel socket state on sk from parent |
| 2198 | */ |
| 2199 | static int smack_socket_getpeersec_dgram(struct socket *sock, |
| 2200 | struct sk_buff *skb, u32 *secid) |
| 2201 | |
| 2202 | { |
| 2203 | struct netlbl_lsm_secattr secattr; |
| 2204 | struct sock *sk; |
| 2205 | char smack[SMK_LABELLEN]; |
| 2206 | int family = PF_INET; |
| 2207 | u32 s; |
| 2208 | int rc; |
| 2209 | |
| 2210 | /* |
| 2211 | * Only works for families with packets. |
| 2212 | */ |
| 2213 | if (sock != NULL) { |
| 2214 | sk = sock->sk; |
| 2215 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) |
| 2216 | return 0; |
| 2217 | family = sk->sk_family; |
| 2218 | } |
| 2219 | /* |
| 2220 | * Translate what netlabel gave us. |
| 2221 | */ |
| 2222 | memset(smack, '\0', SMK_LABELLEN); |
| 2223 | netlbl_secattr_init(&secattr); |
| 2224 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
| 2225 | if (rc == 0) |
| 2226 | smack_from_secattr(&secattr, smack); |
| 2227 | netlbl_secattr_destroy(&secattr); |
| 2228 | |
| 2229 | /* |
| 2230 | * Give up if we couldn't get anything |
| 2231 | */ |
| 2232 | if (rc != 0) |
| 2233 | return rc; |
| 2234 | |
| 2235 | s = smack_to_secid(smack); |
| 2236 | if (s == 0) |
| 2237 | return -EINVAL; |
| 2238 | |
| 2239 | *secid = s; |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | /** |
| 2244 | * smack_sock_graft - graft access state between two sockets |
| 2245 | * @sk: fresh sock |
| 2246 | * @parent: donor socket |
| 2247 | * |
| 2248 | * Sets the netlabel socket state on sk from parent |
| 2249 | */ |
| 2250 | static void smack_sock_graft(struct sock *sk, struct socket *parent) |
| 2251 | { |
| 2252 | struct socket_smack *ssp; |
| 2253 | int rc; |
| 2254 | |
| 2255 | if (sk == NULL) |
| 2256 | return; |
| 2257 | |
| 2258 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) |
| 2259 | return; |
| 2260 | |
| 2261 | ssp = sk->sk_security; |
| 2262 | ssp->smk_in = current->security; |
| 2263 | ssp->smk_out = current->security; |
| 2264 | ssp->smk_packet[0] = '\0'; |
| 2265 | |
| 2266 | rc = smack_netlabel(sk); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2267 | if (rc != 0) |
| 2268 | printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n", |
| 2269 | __func__, -rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /** |
| 2273 | * smack_inet_conn_request - Smack access check on connect |
| 2274 | * @sk: socket involved |
| 2275 | * @skb: packet |
| 2276 | * @req: unused |
| 2277 | * |
| 2278 | * Returns 0 if a task with the packet label could write to |
| 2279 | * the socket, otherwise an error code |
| 2280 | */ |
| 2281 | static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 2282 | struct request_sock *req) |
| 2283 | { |
| 2284 | struct netlbl_lsm_secattr skb_secattr; |
| 2285 | struct socket_smack *ssp = sk->sk_security; |
| 2286 | char smack[SMK_LABELLEN]; |
| 2287 | int rc; |
| 2288 | |
| 2289 | if (skb == NULL) |
| 2290 | return -EACCES; |
| 2291 | |
| 2292 | memset(smack, '\0', SMK_LABELLEN); |
| 2293 | netlbl_secattr_init(&skb_secattr); |
| 2294 | rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr); |
| 2295 | if (rc == 0) |
| 2296 | smack_from_secattr(&skb_secattr, smack); |
| 2297 | else |
| 2298 | strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN); |
| 2299 | netlbl_secattr_destroy(&skb_secattr); |
| 2300 | /* |
| 2301 | * Receiving a packet requires that the other end |
| 2302 | * be able to write here. Read access is not required. |
| 2303 | * |
| 2304 | * If the request is successful save the peer's label |
| 2305 | * so that SO_PEERCRED can report it. |
| 2306 | */ |
| 2307 | rc = smk_access(smack, ssp->smk_in, MAY_WRITE); |
| 2308 | if (rc == 0) |
| 2309 | strncpy(ssp->smk_packet, smack, SMK_MAXLEN); |
| 2310 | |
| 2311 | return rc; |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | * Key management security hooks |
| 2316 | * |
| 2317 | * Casey has not tested key support very heavily. |
| 2318 | * The permission check is most likely too restrictive. |
| 2319 | * If you care about keys please have a look. |
| 2320 | */ |
| 2321 | #ifdef CONFIG_KEYS |
| 2322 | |
| 2323 | /** |
| 2324 | * smack_key_alloc - Set the key security blob |
| 2325 | * @key: object |
| 2326 | * @tsk: the task associated with the key |
| 2327 | * @flags: unused |
| 2328 | * |
| 2329 | * No allocation required |
| 2330 | * |
| 2331 | * Returns 0 |
| 2332 | */ |
| 2333 | static int smack_key_alloc(struct key *key, struct task_struct *tsk, |
| 2334 | unsigned long flags) |
| 2335 | { |
| 2336 | key->security = tsk->security; |
| 2337 | return 0; |
| 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * smack_key_free - Clear the key security blob |
| 2342 | * @key: the object |
| 2343 | * |
| 2344 | * Clear the blob pointer |
| 2345 | */ |
| 2346 | static void smack_key_free(struct key *key) |
| 2347 | { |
| 2348 | key->security = NULL; |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * smack_key_permission - Smack access on a key |
| 2353 | * @key_ref: gets to the object |
| 2354 | * @context: task involved |
| 2355 | * @perm: unused |
| 2356 | * |
| 2357 | * Return 0 if the task has read and write to the object, |
| 2358 | * an error code otherwise |
| 2359 | */ |
| 2360 | static int smack_key_permission(key_ref_t key_ref, |
| 2361 | struct task_struct *context, key_perm_t perm) |
| 2362 | { |
| 2363 | struct key *keyp; |
| 2364 | |
| 2365 | keyp = key_ref_to_ptr(key_ref); |
| 2366 | if (keyp == NULL) |
| 2367 | return -EINVAL; |
| 2368 | /* |
| 2369 | * If the key hasn't been initialized give it access so that |
| 2370 | * it may do so. |
| 2371 | */ |
| 2372 | if (keyp->security == NULL) |
| 2373 | return 0; |
| 2374 | /* |
| 2375 | * This should not occur |
| 2376 | */ |
| 2377 | if (context->security == NULL) |
| 2378 | return -EACCES; |
| 2379 | |
| 2380 | return smk_access(context->security, keyp->security, MAY_READWRITE); |
| 2381 | } |
| 2382 | #endif /* CONFIG_KEYS */ |
| 2383 | |
| 2384 | /* |
| 2385 | * smack_secid_to_secctx - return the smack label for a secid |
| 2386 | * @secid: incoming integer |
| 2387 | * @secdata: destination |
| 2388 | * @seclen: how long it is |
| 2389 | * |
| 2390 | * Exists for networking code. |
| 2391 | */ |
| 2392 | static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 2393 | { |
| 2394 | char *sp = smack_from_secid(secid); |
| 2395 | |
| 2396 | *secdata = sp; |
| 2397 | *seclen = strlen(sp); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | /* |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2402 | * smack_secctx_to_secid - return the secid for a smack label |
| 2403 | * @secdata: smack label |
| 2404 | * @seclen: how long result is |
| 2405 | * @secid: outgoing integer |
| 2406 | * |
| 2407 | * Exists for audit and networking code. |
| 2408 | */ |
| 2409 | static int smack_secctx_to_secid(char *secdata, u32 seclen, u32 *secid) |
| 2410 | { |
| 2411 | *secid = smack_to_secid(secdata); |
| 2412 | return 0; |
| 2413 | } |
| 2414 | |
| 2415 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2416 | * smack_release_secctx - don't do anything. |
| 2417 | * @key_ref: unused |
| 2418 | * @context: unused |
| 2419 | * @perm: unused |
| 2420 | * |
| 2421 | * Exists to make sure nothing gets done, and properly |
| 2422 | */ |
| 2423 | static void smack_release_secctx(char *secdata, u32 seclen) |
| 2424 | { |
| 2425 | } |
| 2426 | |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 2427 | struct security_operations smack_ops = { |
| 2428 | .name = "smack", |
| 2429 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2430 | .ptrace = smack_ptrace, |
| 2431 | .capget = cap_capget, |
| 2432 | .capset_check = cap_capset_check, |
| 2433 | .capset_set = cap_capset_set, |
| 2434 | .capable = cap_capable, |
| 2435 | .syslog = smack_syslog, |
| 2436 | .settime = cap_settime, |
| 2437 | .vm_enough_memory = cap_vm_enough_memory, |
| 2438 | |
| 2439 | .bprm_apply_creds = cap_bprm_apply_creds, |
| 2440 | .bprm_set_security = cap_bprm_set_security, |
| 2441 | .bprm_secureexec = cap_bprm_secureexec, |
| 2442 | |
| 2443 | .sb_alloc_security = smack_sb_alloc_security, |
| 2444 | .sb_free_security = smack_sb_free_security, |
| 2445 | .sb_copy_data = smack_sb_copy_data, |
| 2446 | .sb_kern_mount = smack_sb_kern_mount, |
| 2447 | .sb_statfs = smack_sb_statfs, |
| 2448 | .sb_mount = smack_sb_mount, |
| 2449 | .sb_umount = smack_sb_umount, |
| 2450 | |
| 2451 | .inode_alloc_security = smack_inode_alloc_security, |
| 2452 | .inode_free_security = smack_inode_free_security, |
| 2453 | .inode_init_security = smack_inode_init_security, |
| 2454 | .inode_link = smack_inode_link, |
| 2455 | .inode_unlink = smack_inode_unlink, |
| 2456 | .inode_rmdir = smack_inode_rmdir, |
| 2457 | .inode_rename = smack_inode_rename, |
| 2458 | .inode_permission = smack_inode_permission, |
| 2459 | .inode_setattr = smack_inode_setattr, |
| 2460 | .inode_getattr = smack_inode_getattr, |
| 2461 | .inode_setxattr = smack_inode_setxattr, |
| 2462 | .inode_post_setxattr = smack_inode_post_setxattr, |
| 2463 | .inode_getxattr = smack_inode_getxattr, |
| 2464 | .inode_removexattr = smack_inode_removexattr, |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 2465 | .inode_need_killpriv = cap_inode_need_killpriv, |
| 2466 | .inode_killpriv = cap_inode_killpriv, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2467 | .inode_getsecurity = smack_inode_getsecurity, |
| 2468 | .inode_setsecurity = smack_inode_setsecurity, |
| 2469 | .inode_listsecurity = smack_inode_listsecurity, |
| 2470 | |
| 2471 | .file_permission = smack_file_permission, |
| 2472 | .file_alloc_security = smack_file_alloc_security, |
| 2473 | .file_free_security = smack_file_free_security, |
| 2474 | .file_ioctl = smack_file_ioctl, |
| 2475 | .file_lock = smack_file_lock, |
| 2476 | .file_fcntl = smack_file_fcntl, |
| 2477 | .file_set_fowner = smack_file_set_fowner, |
| 2478 | .file_send_sigiotask = smack_file_send_sigiotask, |
| 2479 | .file_receive = smack_file_receive, |
| 2480 | |
| 2481 | .task_alloc_security = smack_task_alloc_security, |
| 2482 | .task_free_security = smack_task_free_security, |
| 2483 | .task_post_setuid = cap_task_post_setuid, |
| 2484 | .task_setpgid = smack_task_setpgid, |
| 2485 | .task_getpgid = smack_task_getpgid, |
| 2486 | .task_getsid = smack_task_getsid, |
| 2487 | .task_getsecid = smack_task_getsecid, |
| 2488 | .task_setnice = smack_task_setnice, |
| 2489 | .task_setioprio = smack_task_setioprio, |
| 2490 | .task_getioprio = smack_task_getioprio, |
| 2491 | .task_setscheduler = smack_task_setscheduler, |
| 2492 | .task_getscheduler = smack_task_getscheduler, |
| 2493 | .task_movememory = smack_task_movememory, |
| 2494 | .task_kill = smack_task_kill, |
| 2495 | .task_wait = smack_task_wait, |
| 2496 | .task_reparent_to_init = cap_task_reparent_to_init, |
| 2497 | .task_to_inode = smack_task_to_inode, |
Serge E. Hallyn | 1236cc3 | 2008-04-28 02:13:43 -0700 | [diff] [blame] | 2498 | .task_prctl = cap_task_prctl, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2499 | |
| 2500 | .ipc_permission = smack_ipc_permission, |
| 2501 | |
| 2502 | .msg_msg_alloc_security = smack_msg_msg_alloc_security, |
| 2503 | .msg_msg_free_security = smack_msg_msg_free_security, |
| 2504 | |
| 2505 | .msg_queue_alloc_security = smack_msg_queue_alloc_security, |
| 2506 | .msg_queue_free_security = smack_msg_queue_free_security, |
| 2507 | .msg_queue_associate = smack_msg_queue_associate, |
| 2508 | .msg_queue_msgctl = smack_msg_queue_msgctl, |
| 2509 | .msg_queue_msgsnd = smack_msg_queue_msgsnd, |
| 2510 | .msg_queue_msgrcv = smack_msg_queue_msgrcv, |
| 2511 | |
| 2512 | .shm_alloc_security = smack_shm_alloc_security, |
| 2513 | .shm_free_security = smack_shm_free_security, |
| 2514 | .shm_associate = smack_shm_associate, |
| 2515 | .shm_shmctl = smack_shm_shmctl, |
| 2516 | .shm_shmat = smack_shm_shmat, |
| 2517 | |
| 2518 | .sem_alloc_security = smack_sem_alloc_security, |
| 2519 | .sem_free_security = smack_sem_free_security, |
| 2520 | .sem_associate = smack_sem_associate, |
| 2521 | .sem_semctl = smack_sem_semctl, |
| 2522 | .sem_semop = smack_sem_semop, |
| 2523 | |
| 2524 | .netlink_send = cap_netlink_send, |
| 2525 | .netlink_recv = cap_netlink_recv, |
| 2526 | |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 2527 | .register_security = smack_register_security, |
| 2528 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2529 | .d_instantiate = smack_d_instantiate, |
| 2530 | |
| 2531 | .getprocattr = smack_getprocattr, |
| 2532 | .setprocattr = smack_setprocattr, |
| 2533 | |
| 2534 | .unix_stream_connect = smack_unix_stream_connect, |
| 2535 | .unix_may_send = smack_unix_may_send, |
| 2536 | |
| 2537 | .socket_post_create = smack_socket_post_create, |
| 2538 | .socket_sock_rcv_skb = smack_socket_sock_rcv_skb, |
| 2539 | .socket_getpeersec_stream = smack_socket_getpeersec_stream, |
| 2540 | .socket_getpeersec_dgram = smack_socket_getpeersec_dgram, |
| 2541 | .sk_alloc_security = smack_sk_alloc_security, |
| 2542 | .sk_free_security = smack_sk_free_security, |
| 2543 | .sock_graft = smack_sock_graft, |
| 2544 | .inet_conn_request = smack_inet_conn_request, |
| 2545 | /* key management security hooks */ |
| 2546 | #ifdef CONFIG_KEYS |
| 2547 | .key_alloc = smack_key_alloc, |
| 2548 | .key_free = smack_key_free, |
| 2549 | .key_permission = smack_key_permission, |
| 2550 | #endif /* CONFIG_KEYS */ |
| 2551 | .secid_to_secctx = smack_secid_to_secctx, |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2552 | .secctx_to_secid = smack_secctx_to_secid, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2553 | .release_secctx = smack_release_secctx, |
| 2554 | }; |
| 2555 | |
| 2556 | /** |
| 2557 | * smack_init - initialize the smack system |
| 2558 | * |
| 2559 | * Returns 0 |
| 2560 | */ |
| 2561 | static __init int smack_init(void) |
| 2562 | { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 2563 | if (!security_module_enable(&smack_ops)) |
| 2564 | return 0; |
| 2565 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2566 | printk(KERN_INFO "Smack: Initializing.\n"); |
| 2567 | |
| 2568 | /* |
| 2569 | * Set the security state for the initial task. |
| 2570 | */ |
| 2571 | current->security = &smack_known_floor.smk_known; |
| 2572 | |
| 2573 | /* |
| 2574 | * Initialize locks |
| 2575 | */ |
| 2576 | spin_lock_init(&smack_known_unset.smk_cipsolock); |
| 2577 | spin_lock_init(&smack_known_huh.smk_cipsolock); |
| 2578 | spin_lock_init(&smack_known_hat.smk_cipsolock); |
| 2579 | spin_lock_init(&smack_known_star.smk_cipsolock); |
| 2580 | spin_lock_init(&smack_known_floor.smk_cipsolock); |
| 2581 | spin_lock_init(&smack_known_invalid.smk_cipsolock); |
| 2582 | |
| 2583 | /* |
| 2584 | * Register with LSM |
| 2585 | */ |
| 2586 | if (register_security(&smack_ops)) |
| 2587 | panic("smack: Unable to register with kernel.\n"); |
| 2588 | |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | /* |
| 2593 | * Smack requires early initialization in order to label |
| 2594 | * all processes and objects when they are created. |
| 2595 | */ |
| 2596 | security_initcall(smack_init); |
| 2597 | |