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 | * |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 6 | * Authors: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 7 | * Casey Schaufler <casey@schaufler-ca.com> |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 8 | * Jarkko Sakkinen <jarkko.sakkinen@intel.com> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 11 | * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 12 | * Paul Moore <paul@paul-moore.com> |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 13 | * Copyright (C) 2010 Nokia Corporation |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 14 | * Copyright (C) 2011 Intel Corporation. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License version 2, |
| 18 | * as published by the Free Software Foundation. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/xattr.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/mount.h> |
| 24 | #include <linux/stat.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 25 | #include <linux/kd.h> |
| 26 | #include <asm/ioctls.h> |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 27 | #include <linux/ip.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 28 | #include <linux/tcp.h> |
| 29 | #include <linux/udp.h> |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 30 | #include <linux/dccp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 32 | #include <linux/mutex.h> |
| 33 | #include <linux/pipe_fs_i.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 34 | #include <net/cipso_ipv4.h> |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 35 | #include <net/ip.h> |
| 36 | #include <net/ipv6.h> |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 37 | #include <linux/audit.h> |
Nick Black | 1fd7317d | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 38 | #include <linux/magic.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 39 | #include <linux/dcache.h> |
Jarkko Sakkinen | 16014d8 | 2011-10-14 13:16:24 +0300 | [diff] [blame] | 40 | #include <linux/personality.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 41 | #include <linux/msg.h> |
| 42 | #include <linux/shm.h> |
| 43 | #include <linux/binfmts.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 44 | #include "smack.h" |
| 45 | |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 46 | #define TRANS_TRUE "TRUE" |
| 47 | #define TRANS_TRUE_SIZE 4 |
| 48 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 49 | #define SMK_CONNECTING 0 |
| 50 | #define SMK_RECEIVING 1 |
| 51 | #define SMK_SENDING 2 |
| 52 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 53 | #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 54 | LIST_HEAD(smk_ipv6_port_list); |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 55 | #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */ |
Rohit | 1a5b472 | 2014-10-15 17:40:41 +0530 | [diff] [blame] | 56 | static struct kmem_cache *smack_inode_cache; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 57 | int smack_enabled; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 58 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 60 | static char *smk_bu_mess[] = { |
| 61 | "Bringup Error", /* Unused */ |
| 62 | "Bringup", /* SMACK_BRINGUP_ALLOW */ |
| 63 | "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */ |
| 64 | "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */ |
| 65 | }; |
| 66 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 67 | static void smk_bu_mode(int mode, char *s) |
| 68 | { |
| 69 | int i = 0; |
| 70 | |
| 71 | if (mode & MAY_READ) |
| 72 | s[i++] = 'r'; |
| 73 | if (mode & MAY_WRITE) |
| 74 | s[i++] = 'w'; |
| 75 | if (mode & MAY_EXEC) |
| 76 | s[i++] = 'x'; |
| 77 | if (mode & MAY_APPEND) |
| 78 | s[i++] = 'a'; |
| 79 | if (mode & MAY_TRANSMUTE) |
| 80 | s[i++] = 't'; |
| 81 | if (mode & MAY_LOCK) |
| 82 | s[i++] = 'l'; |
| 83 | if (i == 0) |
| 84 | s[i++] = '-'; |
| 85 | s[i] = '\0'; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 90 | static int smk_bu_note(char *note, struct smack_known *sskp, |
| 91 | struct smack_known *oskp, int mode, int rc) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 92 | { |
| 93 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 94 | |
| 95 | if (rc <= 0) |
| 96 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 97 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 98 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 99 | |
| 100 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 101 | pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc], |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 102 | sskp->smk_known, oskp->smk_known, acc, note); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | #else |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 106 | #define smk_bu_note(note, sskp, oskp, mode, RC) (RC) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 110 | static int smk_bu_current(char *note, struct smack_known *oskp, |
| 111 | int mode, int rc) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 112 | { |
| 113 | struct task_smack *tsp = current_security(); |
| 114 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 115 | |
| 116 | if (rc <= 0) |
| 117 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 118 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 119 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 120 | |
| 121 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 122 | pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc], |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 123 | tsp->smk_task->smk_known, oskp->smk_known, |
| 124 | acc, current->comm, note); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | #else |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 128 | #define smk_bu_current(note, oskp, mode, RC) (RC) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 132 | static int smk_bu_task(struct task_struct *otp, int mode, int rc) |
| 133 | { |
| 134 | struct task_smack *tsp = current_security(); |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 135 | struct smack_known *smk_task = smk_of_task_struct(otp); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 136 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 137 | |
| 138 | if (rc <= 0) |
| 139 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 140 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 141 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 142 | |
| 143 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 144 | pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc], |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 145 | tsp->smk_task->smk_known, smk_task->smk_known, acc, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 146 | current->comm, otp->comm); |
| 147 | return 0; |
| 148 | } |
| 149 | #else |
| 150 | #define smk_bu_task(otp, mode, RC) (RC) |
| 151 | #endif |
| 152 | |
| 153 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 154 | static int smk_bu_inode(struct inode *inode, int mode, int rc) |
| 155 | { |
| 156 | struct task_smack *tsp = current_security(); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 157 | struct inode_smack *isp = inode->i_security; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 158 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 159 | |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 160 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| 161 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", |
| 162 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 163 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 164 | if (rc <= 0) |
| 165 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 166 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 167 | rc = 0; |
| 168 | if (rc == SMACK_UNCONFINED_SUBJECT && |
| 169 | (mode & (MAY_WRITE | MAY_APPEND))) |
| 170 | isp->smk_flags |= SMK_INODE_IMPURE; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 171 | |
| 172 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 173 | |
| 174 | pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc], |
| 175 | tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 176 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 177 | return 0; |
| 178 | } |
| 179 | #else |
| 180 | #define smk_bu_inode(inode, mode, RC) (RC) |
| 181 | #endif |
| 182 | |
| 183 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 184 | static int smk_bu_file(struct file *file, int mode, int rc) |
| 185 | { |
| 186 | struct task_smack *tsp = current_security(); |
| 187 | struct smack_known *sskp = tsp->smk_task; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 188 | struct inode *inode = file_inode(file); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 189 | struct inode_smack *isp = inode->i_security; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 190 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 191 | |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 192 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| 193 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", |
| 194 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 195 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 196 | if (rc <= 0) |
| 197 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 198 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 199 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 200 | |
| 201 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 202 | pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 203 | sskp->smk_known, smk_of_inode(inode)->smk_known, acc, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 204 | inode->i_sb->s_id, inode->i_ino, file, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 205 | current->comm); |
| 206 | return 0; |
| 207 | } |
| 208 | #else |
| 209 | #define smk_bu_file(file, mode, RC) (RC) |
| 210 | #endif |
| 211 | |
| 212 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 213 | static int smk_bu_credfile(const struct cred *cred, struct file *file, |
| 214 | int mode, int rc) |
| 215 | { |
| 216 | struct task_smack *tsp = cred->security; |
| 217 | struct smack_known *sskp = tsp->smk_task; |
| 218 | struct inode *inode = file->f_inode; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 219 | struct inode_smack *isp = inode->i_security; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 220 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 221 | |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 222 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| 223 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", |
| 224 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 225 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 226 | if (rc <= 0) |
| 227 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 228 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 229 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 230 | |
| 231 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 232 | pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 233 | sskp->smk_known, smk_of_inode(inode)->smk_known, acc, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 234 | inode->i_sb->s_id, inode->i_ino, file, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 235 | current->comm); |
| 236 | return 0; |
| 237 | } |
| 238 | #else |
| 239 | #define smk_bu_credfile(cred, file, mode, RC) (RC) |
| 240 | #endif |
| 241 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 242 | /** |
| 243 | * smk_fetch - Fetch the smack label from a file. |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 244 | * @name: type of the label (attribute) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 245 | * @ip: a pointer to the inode |
| 246 | * @dp: a pointer to the dentry |
| 247 | * |
| 248 | * Returns a pointer to the master list entry for the Smack label |
| 249 | * or NULL if there was no label to fetch. |
| 250 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 251 | static struct smack_known *smk_fetch(const char *name, struct inode *ip, |
| 252 | struct dentry *dp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 253 | { |
| 254 | int rc; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 255 | char *buffer; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 256 | struct smack_known *skp = NULL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 257 | |
| 258 | if (ip->i_op->getxattr == NULL) |
| 259 | return NULL; |
| 260 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 261 | buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL); |
| 262 | if (buffer == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 263 | return NULL; |
| 264 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 265 | rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL); |
| 266 | if (rc > 0) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 267 | skp = smk_import_entry(buffer, rc); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 268 | |
| 269 | kfree(buffer); |
| 270 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 271 | return skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * new_inode_smack - allocate an inode security blob |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 276 | * @skp: a pointer to the Smack label entry to use in the blob |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 277 | * |
| 278 | * Returns the new blob or NULL if there's no memory available |
| 279 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 280 | struct inode_smack *new_inode_smack(struct smack_known *skp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 281 | { |
| 282 | struct inode_smack *isp; |
| 283 | |
Rohit | 1a5b472 | 2014-10-15 17:40:41 +0530 | [diff] [blame] | 284 | isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 285 | if (isp == NULL) |
| 286 | return NULL; |
| 287 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 288 | isp->smk_inode = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 289 | isp->smk_flags = 0; |
| 290 | mutex_init(&isp->smk_lock); |
| 291 | |
| 292 | return isp; |
| 293 | } |
| 294 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 295 | /** |
| 296 | * new_task_smack - allocate a task security blob |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 297 | * @task: a pointer to the Smack label for the running task |
| 298 | * @forked: a pointer to the Smack label for the forked task |
| 299 | * @gfp: type of the memory for the allocation |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 300 | * |
| 301 | * Returns the new blob or NULL if there's no memory available |
| 302 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 303 | static struct task_smack *new_task_smack(struct smack_known *task, |
| 304 | struct smack_known *forked, gfp_t gfp) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 305 | { |
| 306 | struct task_smack *tsp; |
| 307 | |
| 308 | tsp = kzalloc(sizeof(struct task_smack), gfp); |
| 309 | if (tsp == NULL) |
| 310 | return NULL; |
| 311 | |
| 312 | tsp->smk_task = task; |
| 313 | tsp->smk_forked = forked; |
| 314 | INIT_LIST_HEAD(&tsp->smk_rules); |
| 315 | mutex_init(&tsp->smk_rules_lock); |
| 316 | |
| 317 | return tsp; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * smk_copy_rules - copy a rule set |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 322 | * @nhead: new rules header pointer |
| 323 | * @ohead: old rules header pointer |
| 324 | * @gfp: type of the memory for the allocation |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 325 | * |
| 326 | * Returns 0 on success, -ENOMEM on error |
| 327 | */ |
| 328 | static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead, |
| 329 | gfp_t gfp) |
| 330 | { |
| 331 | struct smack_rule *nrp; |
| 332 | struct smack_rule *orp; |
| 333 | int rc = 0; |
| 334 | |
| 335 | INIT_LIST_HEAD(nhead); |
| 336 | |
| 337 | list_for_each_entry_rcu(orp, ohead, list) { |
| 338 | nrp = kzalloc(sizeof(struct smack_rule), gfp); |
| 339 | if (nrp == NULL) { |
| 340 | rc = -ENOMEM; |
| 341 | break; |
| 342 | } |
| 343 | *nrp = *orp; |
| 344 | list_add_rcu(&nrp->list, nhead); |
| 345 | } |
| 346 | return rc; |
| 347 | } |
| 348 | |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 349 | /** |
| 350 | * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_* |
| 351 | * @mode - input mode in form of PTRACE_MODE_* |
| 352 | * |
| 353 | * Returns a converted MAY_* mode usable by smack rules |
| 354 | */ |
| 355 | static inline unsigned int smk_ptrace_mode(unsigned int mode) |
| 356 | { |
| 357 | switch (mode) { |
| 358 | case PTRACE_MODE_READ: |
| 359 | return MAY_READ; |
| 360 | case PTRACE_MODE_ATTACH: |
| 361 | return MAY_READWRITE; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * smk_ptrace_rule_check - helper for ptrace access |
| 369 | * @tracer: tracer process |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 370 | * @tracee_known: label entry of the process that's about to be traced |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 371 | * @mode: ptrace attachment mode (PTRACE_MODE_*) |
| 372 | * @func: name of the function that called us, used for audit |
| 373 | * |
| 374 | * Returns 0 on access granted, -error on error |
| 375 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 376 | static int smk_ptrace_rule_check(struct task_struct *tracer, |
| 377 | struct smack_known *tracee_known, |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 378 | unsigned int mode, const char *func) |
| 379 | { |
| 380 | int rc; |
| 381 | struct smk_audit_info ad, *saip = NULL; |
| 382 | struct task_smack *tsp; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 383 | struct smack_known *tracer_known; |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 384 | |
| 385 | if ((mode & PTRACE_MODE_NOAUDIT) == 0) { |
| 386 | smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK); |
| 387 | smk_ad_setfield_u_tsk(&ad, tracer); |
| 388 | saip = &ad; |
| 389 | } |
| 390 | |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 391 | rcu_read_lock(); |
| 392 | tsp = __task_cred(tracer)->security; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 393 | tracer_known = smk_of_task(tsp); |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 394 | |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 395 | if ((mode & PTRACE_MODE_ATTACH) && |
| 396 | (smack_ptrace_rule == SMACK_PTRACE_EXACT || |
| 397 | smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 398 | if (tracer_known->smk_known == tracee_known->smk_known) |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 399 | rc = 0; |
| 400 | else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN) |
| 401 | rc = -EACCES; |
| 402 | else if (capable(CAP_SYS_PTRACE)) |
| 403 | rc = 0; |
| 404 | else |
| 405 | rc = -EACCES; |
| 406 | |
| 407 | if (saip) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 408 | smack_log(tracer_known->smk_known, |
| 409 | tracee_known->smk_known, |
| 410 | 0, rc, saip); |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 411 | |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 412 | rcu_read_unlock(); |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 413 | return rc; |
| 414 | } |
| 415 | |
| 416 | /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 417 | rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip); |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 418 | |
| 419 | rcu_read_unlock(); |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 420 | return rc; |
| 421 | } |
| 422 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 423 | /* |
| 424 | * LSM hooks. |
| 425 | * We he, that is fun! |
| 426 | */ |
| 427 | |
| 428 | /** |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 429 | * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 430 | * @ctp: child task pointer |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 431 | * @mode: ptrace attachment mode (PTRACE_MODE_*) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 432 | * |
| 433 | * Returns 0 if access is OK, an error code otherwise |
| 434 | * |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 435 | * Do the capability checks. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 436 | */ |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 437 | static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 438 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 439 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 440 | |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 441 | skp = smk_of_task_struct(ctp); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 442 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 443 | return smk_ptrace_rule_check(current, skp, mode, __func__); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 444 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 445 | |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 446 | /** |
| 447 | * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME |
| 448 | * @ptp: parent task pointer |
| 449 | * |
| 450 | * Returns 0 if access is OK, an error code otherwise |
| 451 | * |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 452 | * Do the capability checks, and require PTRACE_MODE_ATTACH. |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 453 | */ |
| 454 | static int smack_ptrace_traceme(struct task_struct *ptp) |
| 455 | { |
| 456 | int rc; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 457 | struct smack_known *skp; |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 458 | |
Lukasz Pawelczyk | 959e6c7 | 2014-03-11 17:07:04 +0100 | [diff] [blame] | 459 | skp = smk_of_task(current_security()); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 460 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 461 | rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 462 | return rc; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * smack_syslog - Smack approval on syslog |
| 467 | * @type: message type |
| 468 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 469 | * Returns 0 on success, error code otherwise. |
| 470 | */ |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 471 | static int smack_syslog(int typefrom_file) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 472 | { |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 473 | int rc = 0; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 474 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 475 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 476 | if (smack_privileged(CAP_MAC_OVERRIDE)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 477 | return 0; |
| 478 | |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 479 | if (smack_syslog_label != NULL && smack_syslog_label != skp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 480 | rc = -EACCES; |
| 481 | |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /* |
| 487 | * Superblock Hooks. |
| 488 | */ |
| 489 | |
| 490 | /** |
| 491 | * smack_sb_alloc_security - allocate a superblock blob |
| 492 | * @sb: the superblock getting the blob |
| 493 | * |
| 494 | * Returns 0 on success or -ENOMEM on error. |
| 495 | */ |
| 496 | static int smack_sb_alloc_security(struct super_block *sb) |
| 497 | { |
| 498 | struct superblock_smack *sbsp; |
| 499 | |
| 500 | sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL); |
| 501 | |
| 502 | if (sbsp == NULL) |
| 503 | return -ENOMEM; |
| 504 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 505 | sbsp->smk_root = &smack_known_floor; |
| 506 | sbsp->smk_default = &smack_known_floor; |
| 507 | sbsp->smk_floor = &smack_known_floor; |
| 508 | sbsp->smk_hat = &smack_known_hat; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 509 | /* |
| 510 | * smk_initialized will be zero from kzalloc. |
| 511 | */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 512 | sb->s_security = sbsp; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * smack_sb_free_security - free a superblock blob |
| 519 | * @sb: the superblock getting the blob |
| 520 | * |
| 521 | */ |
| 522 | static void smack_sb_free_security(struct super_block *sb) |
| 523 | { |
| 524 | kfree(sb->s_security); |
| 525 | sb->s_security = NULL; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * smack_sb_copy_data - copy mount options data for processing |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 530 | * @orig: where to start |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 531 | * @smackopts: mount options string |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 532 | * |
| 533 | * Returns 0 on success or -ENOMEM on error. |
| 534 | * |
| 535 | * Copy the Smack specific mount options out of the mount |
| 536 | * options list. |
| 537 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 538 | static int smack_sb_copy_data(char *orig, char *smackopts) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 539 | { |
| 540 | char *cp, *commap, *otheropts, *dp; |
| 541 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 542 | otheropts = (char *)get_zeroed_page(GFP_KERNEL); |
| 543 | if (otheropts == NULL) |
| 544 | return -ENOMEM; |
| 545 | |
| 546 | for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) { |
| 547 | if (strstr(cp, SMK_FSDEFAULT) == cp) |
| 548 | dp = smackopts; |
| 549 | else if (strstr(cp, SMK_FSFLOOR) == cp) |
| 550 | dp = smackopts; |
| 551 | else if (strstr(cp, SMK_FSHAT) == cp) |
| 552 | dp = smackopts; |
| 553 | else if (strstr(cp, SMK_FSROOT) == cp) |
| 554 | dp = smackopts; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 555 | else if (strstr(cp, SMK_FSTRANS) == cp) |
| 556 | dp = smackopts; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 557 | else |
| 558 | dp = otheropts; |
| 559 | |
| 560 | commap = strchr(cp, ','); |
| 561 | if (commap != NULL) |
| 562 | *commap = '\0'; |
| 563 | |
| 564 | if (*dp != '\0') |
| 565 | strcat(dp, ","); |
| 566 | strcat(dp, cp); |
| 567 | } |
| 568 | |
| 569 | strcpy(orig, otheropts); |
| 570 | free_page((unsigned long)otheropts); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * smack_sb_kern_mount - Smack specific mount processing |
| 577 | * @sb: the file system superblock |
James Morris | 12204e2 | 2008-12-19 10:44:42 +1100 | [diff] [blame] | 578 | * @flags: the mount flags |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 579 | * @data: the smack mount options |
| 580 | * |
| 581 | * Returns 0 on success, an error code on failure |
| 582 | */ |
James Morris | 12204e2 | 2008-12-19 10:44:42 +1100 | [diff] [blame] | 583 | static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 584 | { |
| 585 | struct dentry *root = sb->s_root; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 586 | struct inode *inode = d_backing_inode(root); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 587 | struct superblock_smack *sp = sb->s_security; |
| 588 | struct inode_smack *isp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 589 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 590 | char *op; |
| 591 | char *commap; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 592 | int transmute = 0; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 593 | int specified = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 594 | |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 595 | if (sp->smk_initialized) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 596 | return 0; |
Casey Schaufler | eb982cb | 2012-05-23 17:46:58 -0700 | [diff] [blame] | 597 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 598 | sp->smk_initialized = 1; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 599 | |
| 600 | for (op = data; op != NULL; op = commap) { |
| 601 | commap = strchr(op, ','); |
| 602 | if (commap != NULL) |
| 603 | *commap++ = '\0'; |
| 604 | |
| 605 | if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) { |
| 606 | op += strlen(SMK_FSHAT); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 607 | skp = smk_import_entry(op, 0); |
| 608 | if (skp != NULL) { |
| 609 | sp->smk_hat = skp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 610 | specified = 1; |
| 611 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 612 | } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) { |
| 613 | op += strlen(SMK_FSFLOOR); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 614 | skp = smk_import_entry(op, 0); |
| 615 | if (skp != NULL) { |
| 616 | sp->smk_floor = skp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 617 | specified = 1; |
| 618 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 619 | } else if (strncmp(op, SMK_FSDEFAULT, |
| 620 | strlen(SMK_FSDEFAULT)) == 0) { |
| 621 | op += strlen(SMK_FSDEFAULT); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 622 | skp = smk_import_entry(op, 0); |
| 623 | if (skp != NULL) { |
| 624 | sp->smk_default = skp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 625 | specified = 1; |
| 626 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 627 | } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) { |
| 628 | op += strlen(SMK_FSROOT); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 629 | skp = smk_import_entry(op, 0); |
| 630 | if (skp != NULL) { |
| 631 | sp->smk_root = skp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 632 | specified = 1; |
| 633 | } |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 634 | } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) { |
| 635 | op += strlen(SMK_FSTRANS); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 636 | skp = smk_import_entry(op, 0); |
| 637 | if (skp != NULL) { |
| 638 | sp->smk_root = skp; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 639 | transmute = 1; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 640 | specified = 1; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 641 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 645 | if (!smack_privileged(CAP_MAC_ADMIN)) { |
| 646 | /* |
| 647 | * Unprivileged mounts don't get to specify Smack values. |
| 648 | */ |
| 649 | if (specified) |
| 650 | return -EPERM; |
| 651 | /* |
| 652 | * Unprivileged mounts get root and default from the caller. |
| 653 | */ |
| 654 | skp = smk_of_current(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 655 | sp->smk_root = skp; |
| 656 | sp->smk_default = skp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 657 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 658 | /* |
| 659 | * Initialize the root inode. |
| 660 | */ |
| 661 | isp = inode->i_security; |
José Bollo | 55dfc5d | 2014-01-08 15:53:05 +0100 | [diff] [blame] | 662 | if (isp == NULL) { |
| 663 | isp = new_inode_smack(sp->smk_root); |
| 664 | if (isp == NULL) |
| 665 | return -ENOMEM; |
| 666 | inode->i_security = isp; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 667 | } else |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 668 | isp->smk_inode = sp->smk_root; |
| 669 | |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 670 | if (transmute) |
| 671 | isp->smk_flags |= SMK_INODE_TRANSMUTE; |
| 672 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * smack_sb_statfs - Smack check on statfs |
| 678 | * @dentry: identifies the file system in question |
| 679 | * |
| 680 | * Returns 0 if current can read the floor of the filesystem, |
| 681 | * and error code otherwise |
| 682 | */ |
| 683 | static int smack_sb_statfs(struct dentry *dentry) |
| 684 | { |
| 685 | struct superblock_smack *sbp = dentry->d_sb->s_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 686 | int rc; |
| 687 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 688 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 689 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 690 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 691 | |
| 692 | rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 693 | rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 694 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 697 | /* |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 698 | * BPRM hooks |
| 699 | */ |
| 700 | |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 701 | /** |
| 702 | * smack_bprm_set_creds - set creds for exec |
| 703 | * @bprm: the exec information |
| 704 | * |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 705 | * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 706 | */ |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 707 | static int smack_bprm_set_creds(struct linux_binprm *bprm) |
| 708 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 709 | struct inode *inode = file_inode(bprm->file); |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 710 | struct task_smack *bsp = bprm->cred->security; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 711 | struct inode_smack *isp; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 712 | int rc; |
| 713 | |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 714 | if (bprm->cred_prepared) |
| 715 | return 0; |
| 716 | |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 717 | isp = inode->i_security; |
| 718 | if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 719 | return 0; |
| 720 | |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 721 | if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { |
| 722 | struct task_struct *tracer; |
| 723 | rc = 0; |
| 724 | |
| 725 | rcu_read_lock(); |
| 726 | tracer = ptrace_parent(current); |
| 727 | if (likely(tracer != NULL)) |
| 728 | rc = smk_ptrace_rule_check(tracer, |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 729 | isp->smk_task, |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 730 | PTRACE_MODE_ATTACH, |
| 731 | __func__); |
| 732 | rcu_read_unlock(); |
| 733 | |
| 734 | if (rc != 0) |
| 735 | return rc; |
| 736 | } else if (bprm->unsafe) |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 737 | return -EPERM; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 738 | |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 739 | bsp->smk_task = isp->smk_task; |
| 740 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 745 | /** |
| 746 | * smack_bprm_committing_creds - Prepare to install the new credentials |
| 747 | * from bprm. |
| 748 | * |
| 749 | * @bprm: binprm for exec |
| 750 | */ |
| 751 | static void smack_bprm_committing_creds(struct linux_binprm *bprm) |
| 752 | { |
| 753 | struct task_smack *bsp = bprm->cred->security; |
| 754 | |
| 755 | if (bsp->smk_task != bsp->smk_forked) |
| 756 | current->pdeath_signal = 0; |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * smack_bprm_secureexec - Return the decision to use secureexec. |
| 761 | * @bprm: binprm for exec |
| 762 | * |
| 763 | * Returns 0 on success. |
| 764 | */ |
| 765 | static int smack_bprm_secureexec(struct linux_binprm *bprm) |
| 766 | { |
| 767 | struct task_smack *tsp = current_security(); |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 768 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 769 | if (tsp->smk_task != tsp->smk_forked) |
| 770 | return 1; |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 771 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 772 | return 0; |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 773 | } |
| 774 | |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 775 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 776 | * Inode hooks |
| 777 | */ |
| 778 | |
| 779 | /** |
| 780 | * smack_inode_alloc_security - allocate an inode blob |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 781 | * @inode: the inode in need of a blob |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 782 | * |
| 783 | * Returns 0 if it gets a blob, -ENOMEM otherwise |
| 784 | */ |
| 785 | static int smack_inode_alloc_security(struct inode *inode) |
| 786 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 787 | struct smack_known *skp = smk_of_current(); |
| 788 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 789 | inode->i_security = new_inode_smack(skp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 790 | if (inode->i_security == NULL) |
| 791 | return -ENOMEM; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * smack_inode_free_security - free an inode blob |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 797 | * @inode: the inode with a blob |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 798 | * |
| 799 | * Clears the blob pointer in inode |
| 800 | */ |
| 801 | static void smack_inode_free_security(struct inode *inode) |
| 802 | { |
Rohit | 1a5b472 | 2014-10-15 17:40:41 +0530 | [diff] [blame] | 803 | kmem_cache_free(smack_inode_cache, inode->i_security); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 804 | inode->i_security = NULL; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * smack_inode_init_security - copy out the smack from an inode |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 809 | * @inode: the newly created inode |
| 810 | * @dir: containing directory object |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 811 | * @qstr: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 812 | * @name: where to put the attribute name |
| 813 | * @value: where to put the attribute value |
| 814 | * @len: where to put the length of the attribute |
| 815 | * |
| 816 | * Returns 0 if it all works out, -ENOMEM if there's no memory |
| 817 | */ |
| 818 | static int smack_inode_init_security(struct inode *inode, struct inode *dir, |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 819 | const struct qstr *qstr, const char **name, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 820 | void **value, size_t *len) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 821 | { |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 822 | struct inode_smack *issp = inode->i_security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 823 | struct smack_known *skp = smk_of_current(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 824 | struct smack_known *isp = smk_of_inode(inode); |
| 825 | struct smack_known *dsp = smk_of_inode(dir); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 826 | int may; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 827 | |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 828 | if (name) |
| 829 | *name = XATTR_SMACK_SUFFIX; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 830 | |
Lukasz Pawelczyk | 68390cc | 2014-11-26 15:31:07 +0100 | [diff] [blame] | 831 | if (value && len) { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 832 | rcu_read_lock(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 833 | may = smk_access_entry(skp->smk_known, dsp->smk_known, |
| 834 | &skp->smk_rules); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 835 | rcu_read_unlock(); |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * If the access rule allows transmutation and |
| 839 | * the directory requests transmutation then |
| 840 | * by all means transmute. |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 841 | * Mark the inode as changed. |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 842 | */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 843 | if (may > 0 && ((may & MAY_TRANSMUTE) != 0) && |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 844 | smk_inode_transmutable(dir)) { |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 845 | isp = dsp; |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 846 | issp->smk_flags |= SMK_INODE_CHANGED; |
| 847 | } |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 848 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 849 | *value = kstrdup(isp->smk_known, GFP_NOFS); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 850 | if (*value == NULL) |
| 851 | return -ENOMEM; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 852 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 853 | *len = strlen(isp->smk_known); |
Lukasz Pawelczyk | 68390cc | 2014-11-26 15:31:07 +0100 | [diff] [blame] | 854 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * smack_inode_link - Smack check on link |
| 861 | * @old_dentry: the existing object |
| 862 | * @dir: unused |
| 863 | * @new_dentry: the new object |
| 864 | * |
| 865 | * Returns 0 if access is permitted, an error code otherwise |
| 866 | */ |
| 867 | static int smack_inode_link(struct dentry *old_dentry, struct inode *dir, |
| 868 | struct dentry *new_dentry) |
| 869 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 870 | struct smack_known *isp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 871 | struct smk_audit_info ad; |
| 872 | int rc; |
| 873 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 874 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 875 | smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 876 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 877 | isp = smk_of_inode(d_backing_inode(old_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 878 | rc = smk_curacc(isp, MAY_WRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 879 | rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 880 | |
David Howells | 8802565 | 2015-01-29 12:02:32 +0000 | [diff] [blame] | 881 | if (rc == 0 && d_is_positive(new_dentry)) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 882 | isp = smk_of_inode(d_backing_inode(new_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 883 | smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); |
| 884 | rc = smk_curacc(isp, MAY_WRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 885 | rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | return rc; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * smack_inode_unlink - Smack check on inode deletion |
| 893 | * @dir: containing directory object |
| 894 | * @dentry: file to unlink |
| 895 | * |
| 896 | * Returns 0 if current can write the containing directory |
| 897 | * and the object, error code otherwise |
| 898 | */ |
| 899 | static int smack_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 900 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 901 | struct inode *ip = d_backing_inode(dentry); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 902 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 903 | int rc; |
| 904 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 905 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 906 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 907 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 908 | /* |
| 909 | * You need write access to the thing you're unlinking |
| 910 | */ |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 911 | rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 912 | rc = smk_bu_inode(ip, MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 913 | if (rc == 0) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 914 | /* |
| 915 | * You also need write access to the containing directory |
| 916 | */ |
Igor Zhbanov | cdb56b6 | 2013-03-19 13:49:47 +0400 | [diff] [blame] | 917 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 918 | smk_ad_setfield_u_fs_inode(&ad, dir); |
| 919 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 920 | rc = smk_bu_inode(dir, MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 921 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 922 | return rc; |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * smack_inode_rmdir - Smack check on directory deletion |
| 927 | * @dir: containing directory object |
| 928 | * @dentry: directory to unlink |
| 929 | * |
| 930 | * Returns 0 if current can write the containing directory |
| 931 | * and the directory, error code otherwise |
| 932 | */ |
| 933 | static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 934 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 935 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 936 | int rc; |
| 937 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 938 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 939 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 940 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 941 | /* |
| 942 | * You need write access to the thing you're removing |
| 943 | */ |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 944 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 945 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 946 | if (rc == 0) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 947 | /* |
| 948 | * You also need write access to the containing directory |
| 949 | */ |
Igor Zhbanov | cdb56b6 | 2013-03-19 13:49:47 +0400 | [diff] [blame] | 950 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 951 | smk_ad_setfield_u_fs_inode(&ad, dir); |
| 952 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 953 | rc = smk_bu_inode(dir, MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 954 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 955 | |
| 956 | return rc; |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * smack_inode_rename - Smack check on rename |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 961 | * @old_inode: unused |
| 962 | * @old_dentry: the old object |
| 963 | * @new_inode: unused |
| 964 | * @new_dentry: the new object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 965 | * |
| 966 | * Read and write access is required on both the old and |
| 967 | * new directories. |
| 968 | * |
| 969 | * Returns 0 if access is permitted, an error code otherwise |
| 970 | */ |
| 971 | static int smack_inode_rename(struct inode *old_inode, |
| 972 | struct dentry *old_dentry, |
| 973 | struct inode *new_inode, |
| 974 | struct dentry *new_dentry) |
| 975 | { |
| 976 | int rc; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 977 | struct smack_known *isp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 978 | struct smk_audit_info ad; |
| 979 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 980 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 981 | smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 982 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 983 | isp = smk_of_inode(d_backing_inode(old_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 984 | rc = smk_curacc(isp, MAY_READWRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 985 | rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 986 | |
David Howells | 8802565 | 2015-01-29 12:02:32 +0000 | [diff] [blame] | 987 | if (rc == 0 && d_is_positive(new_dentry)) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 988 | isp = smk_of_inode(d_backing_inode(new_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 989 | smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); |
| 990 | rc = smk_curacc(isp, MAY_READWRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 991 | rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 992 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 993 | return rc; |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * smack_inode_permission - Smack version of permission() |
| 998 | * @inode: the inode in question |
| 999 | * @mask: the access requested |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1000 | * |
| 1001 | * This is the important Smack hook. |
| 1002 | * |
| 1003 | * Returns 0 if access is permitted, -EACCES otherwise |
| 1004 | */ |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 1005 | static int smack_inode_permission(struct inode *inode, int mask) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1006 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1007 | struct smk_audit_info ad; |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 1008 | int no_block = mask & MAY_NOT_BLOCK; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1009 | int rc; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 1010 | |
| 1011 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1012 | /* |
| 1013 | * No permission to check. Existence test. Yup, it's there. |
| 1014 | */ |
| 1015 | if (mask == 0) |
| 1016 | return 0; |
Andi Kleen | 8c9e80e | 2011-04-21 17:23:19 -0700 | [diff] [blame] | 1017 | |
| 1018 | /* May be droppable after audit */ |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 1019 | if (no_block) |
Andi Kleen | 8c9e80e | 2011-04-21 17:23:19 -0700 | [diff] [blame] | 1020 | return -ECHILD; |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1021 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1022 | smk_ad_setfield_u_fs_inode(&ad, inode); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1023 | rc = smk_curacc(smk_of_inode(inode), mask, &ad); |
| 1024 | rc = smk_bu_inode(inode, mask, rc); |
| 1025 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * smack_inode_setattr - Smack check for setting attributes |
| 1030 | * @dentry: the object |
| 1031 | * @iattr: for the force flag |
| 1032 | * |
| 1033 | * Returns 0 if access is permitted, an error code otherwise |
| 1034 | */ |
| 1035 | static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 1036 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1037 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1038 | int rc; |
| 1039 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1040 | /* |
| 1041 | * Need to allow for clearing the setuid bit. |
| 1042 | */ |
| 1043 | if (iattr->ia_valid & ATTR_FORCE) |
| 1044 | return 0; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1045 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1046 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1047 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1048 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1049 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1050 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * smack_inode_getattr - Smack check for getting attributes |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 1055 | * @mnt: vfsmount of the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1056 | * @dentry: the object |
| 1057 | * |
| 1058 | * Returns 0 if access is permitted, an error code otherwise |
| 1059 | */ |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1060 | static int smack_inode_getattr(const struct path *path) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1061 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1062 | struct smk_audit_info ad; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1063 | struct inode *inode = d_backing_inode(path->dentry); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1064 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1065 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1066 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1067 | smk_ad_setfield_u_fs_path(&ad, *path); |
| 1068 | rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); |
| 1069 | rc = smk_bu_inode(inode, MAY_READ, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1070 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * smack_inode_setxattr - Smack check for setting xattrs |
| 1075 | * @dentry: the object |
| 1076 | * @name: name of the attribute |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 1077 | * @value: value of the attribute |
| 1078 | * @size: size of the value |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1079 | * @flags: unused |
| 1080 | * |
| 1081 | * This protects the Smack attribute explicitly. |
| 1082 | * |
| 1083 | * Returns 0 if access is permitted, an error code otherwise |
| 1084 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1085 | static int smack_inode_setxattr(struct dentry *dentry, const char *name, |
| 1086 | const void *value, size_t size, int flags) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1087 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1088 | struct smk_audit_info ad; |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1089 | struct smack_known *skp; |
| 1090 | int check_priv = 0; |
| 1091 | int check_import = 0; |
| 1092 | int check_star = 0; |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1093 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1094 | |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1095 | /* |
| 1096 | * Check label validity here so import won't fail in post_setxattr |
| 1097 | */ |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1098 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
| 1099 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1100 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { |
| 1101 | check_priv = 1; |
| 1102 | check_import = 1; |
| 1103 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || |
| 1104 | strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { |
| 1105 | check_priv = 1; |
| 1106 | check_import = 1; |
| 1107 | check_star = 1; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1108 | } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1109 | check_priv = 1; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1110 | if (size != TRANS_TRUE_SIZE || |
| 1111 | strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0) |
| 1112 | rc = -EINVAL; |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1113 | } else |
| 1114 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 1115 | |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1116 | if (check_priv && !smack_privileged(CAP_MAC_ADMIN)) |
| 1117 | rc = -EPERM; |
| 1118 | |
| 1119 | if (rc == 0 && check_import) { |
Konstantin Khlebnikov | b862e56 | 2014-08-07 20:52:43 +0400 | [diff] [blame] | 1120 | skp = size ? smk_import_entry(value, size) : NULL; |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1121 | if (skp == NULL || (check_star && |
| 1122 | (skp == &smack_known_star || skp == &smack_known_web))) |
| 1123 | rc = -EINVAL; |
| 1124 | } |
| 1125 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1126 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1127 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 1128 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1129 | if (rc == 0) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1130 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1131 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1132 | } |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1133 | |
| 1134 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * smack_inode_post_setxattr - Apply the Smack update approved above |
| 1139 | * @dentry: object |
| 1140 | * @name: attribute name |
| 1141 | * @value: attribute value |
| 1142 | * @size: attribute size |
| 1143 | * @flags: unused |
| 1144 | * |
| 1145 | * Set the pointer in the inode blob to the entry found |
| 1146 | * in the master label list. |
| 1147 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1148 | static void smack_inode_post_setxattr(struct dentry *dentry, const char *name, |
| 1149 | const void *value, size_t size, int flags) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1150 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1151 | struct smack_known *skp; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1152 | struct inode_smack *isp = d_backing_inode(dentry)->i_security; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1153 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1154 | if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { |
| 1155 | isp->smk_flags |= SMK_INODE_TRANSMUTE; |
| 1156 | return; |
| 1157 | } |
| 1158 | |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1159 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { |
José Bollo | 9598f4c | 2014-04-03 13:48:41 +0200 | [diff] [blame] | 1160 | skp = smk_import_entry(value, size); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1161 | if (skp != NULL) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1162 | isp->smk_inode = skp; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1163 | else |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1164 | isp->smk_inode = &smack_known_invalid; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1165 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) { |
José Bollo | 9598f4c | 2014-04-03 13:48:41 +0200 | [diff] [blame] | 1166 | skp = smk_import_entry(value, size); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1167 | if (skp != NULL) |
| 1168 | isp->smk_task = skp; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1169 | else |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1170 | isp->smk_task = &smack_known_invalid; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1171 | } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { |
José Bollo | 9598f4c | 2014-04-03 13:48:41 +0200 | [diff] [blame] | 1172 | skp = smk_import_entry(value, size); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1173 | if (skp != NULL) |
| 1174 | isp->smk_mmap = skp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1175 | else |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1176 | isp->smk_mmap = &smack_known_invalid; |
| 1177 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1178 | |
| 1179 | return; |
| 1180 | } |
| 1181 | |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 1182 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1183 | * smack_inode_getxattr - Smack check on getxattr |
| 1184 | * @dentry: the object |
| 1185 | * @name: unused |
| 1186 | * |
| 1187 | * Returns 0 if access is permitted, an error code otherwise |
| 1188 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1189 | static int smack_inode_getxattr(struct dentry *dentry, const char *name) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1190 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1191 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1192 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1193 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1194 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1195 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 1196 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1197 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad); |
| 1198 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1199 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 1202 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1203 | * smack_inode_removexattr - Smack check on removexattr |
| 1204 | * @dentry: the object |
| 1205 | * @name: name of the attribute |
| 1206 | * |
| 1207 | * Removing the Smack attribute requires CAP_MAC_ADMIN |
| 1208 | * |
| 1209 | * Returns 0 if access is permitted, an error code otherwise |
| 1210 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1211 | static int smack_inode_removexattr(struct dentry *dentry, const char *name) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1212 | { |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1213 | struct inode_smack *isp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1214 | struct smk_audit_info ad; |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1215 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1216 | |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1217 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
| 1218 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1219 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 || |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1220 | strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1221 | strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 || |
Pankaj Kumar | 5e9ab59 | 2013-12-13 15:12:22 +0530 | [diff] [blame] | 1222 | strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1223 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1224 | rc = -EPERM; |
| 1225 | } else |
| 1226 | rc = cap_inode_removexattr(dentry, name); |
| 1227 | |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1228 | if (rc != 0) |
| 1229 | return rc; |
| 1230 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1231 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1232 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1233 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1234 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1235 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1236 | if (rc != 0) |
| 1237 | return rc; |
| 1238 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1239 | isp = d_backing_inode(dentry)->i_security; |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1240 | /* |
| 1241 | * Don't do anything special for these. |
| 1242 | * XATTR_NAME_SMACKIPIN |
| 1243 | * XATTR_NAME_SMACKIPOUT |
| 1244 | * XATTR_NAME_SMACKEXEC |
| 1245 | */ |
| 1246 | if (strcmp(name, XATTR_NAME_SMACK) == 0) |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1247 | isp->smk_task = NULL; |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1248 | else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1249 | isp->smk_mmap = NULL; |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1250 | else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) |
| 1251 | isp->smk_flags &= ~SMK_INODE_TRANSMUTE; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1252 | |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1253 | return 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /** |
| 1257 | * smack_inode_getsecurity - get smack xattrs |
| 1258 | * @inode: the object |
| 1259 | * @name: attribute name |
| 1260 | * @buffer: where to put the result |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1261 | * @alloc: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1262 | * |
| 1263 | * Returns the size of the attribute or an error code |
| 1264 | */ |
| 1265 | static int smack_inode_getsecurity(const struct inode *inode, |
| 1266 | const char *name, void **buffer, |
| 1267 | bool alloc) |
| 1268 | { |
| 1269 | struct socket_smack *ssp; |
| 1270 | struct socket *sock; |
| 1271 | struct super_block *sbp; |
| 1272 | struct inode *ip = (struct inode *)inode; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1273 | struct smack_known *isp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1274 | int ilen; |
| 1275 | int rc = 0; |
| 1276 | |
| 1277 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { |
| 1278 | isp = smk_of_inode(inode); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1279 | ilen = strlen(isp->smk_known); |
| 1280 | *buffer = isp->smk_known; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1281 | return ilen; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * The rest of the Smack xattrs are only on sockets. |
| 1286 | */ |
| 1287 | sbp = ip->i_sb; |
| 1288 | if (sbp->s_magic != SOCKFS_MAGIC) |
| 1289 | return -EOPNOTSUPP; |
| 1290 | |
| 1291 | sock = SOCKET_I(ip); |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 1292 | if (sock == NULL || sock->sk == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1293 | return -EOPNOTSUPP; |
| 1294 | |
| 1295 | ssp = sock->sk->sk_security; |
| 1296 | |
| 1297 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1298 | isp = ssp->smk_in; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1299 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1300 | isp = ssp->smk_out; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1301 | else |
| 1302 | return -EOPNOTSUPP; |
| 1303 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1304 | ilen = strlen(isp->smk_known); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1305 | if (rc == 0) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1306 | *buffer = isp->smk_known; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1307 | rc = ilen; |
| 1308 | } |
| 1309 | |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | /** |
| 1315 | * smack_inode_listsecurity - list the Smack attributes |
| 1316 | * @inode: the object |
| 1317 | * @buffer: where they go |
| 1318 | * @buffer_size: size of buffer |
| 1319 | * |
| 1320 | * Returns 0 on success, -EINVAL otherwise |
| 1321 | */ |
| 1322 | static int smack_inode_listsecurity(struct inode *inode, char *buffer, |
| 1323 | size_t buffer_size) |
| 1324 | { |
Konstantin Khlebnikov | fd5c9d2 | 2014-08-07 20:52:33 +0400 | [diff] [blame] | 1325 | int len = sizeof(XATTR_NAME_SMACK); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1326 | |
Konstantin Khlebnikov | fd5c9d2 | 2014-08-07 20:52:33 +0400 | [diff] [blame] | 1327 | if (buffer != NULL && len <= buffer_size) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1328 | memcpy(buffer, XATTR_NAME_SMACK, len); |
Konstantin Khlebnikov | fd5c9d2 | 2014-08-07 20:52:33 +0400 | [diff] [blame] | 1329 | |
| 1330 | return len; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 1333 | /** |
| 1334 | * smack_inode_getsecid - Extract inode's security id |
| 1335 | * @inode: inode to extract the info from |
| 1336 | * @secid: where result will be saved |
| 1337 | */ |
| 1338 | static void smack_inode_getsecid(const struct inode *inode, u32 *secid) |
| 1339 | { |
| 1340 | struct inode_smack *isp = inode->i_security; |
| 1341 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1342 | *secid = isp->smk_inode->smk_secid; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 1343 | } |
| 1344 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1345 | /* |
| 1346 | * File Hooks |
| 1347 | */ |
| 1348 | |
| 1349 | /** |
| 1350 | * smack_file_permission - Smack check on file operations |
| 1351 | * @file: unused |
| 1352 | * @mask: unused |
| 1353 | * |
| 1354 | * Returns 0 |
| 1355 | * |
| 1356 | * Should access checks be done on each read or write? |
| 1357 | * UNICOS and SELinux say yes. |
| 1358 | * Trusted Solaris, Trusted Irix, and just about everyone else says no. |
| 1359 | * |
| 1360 | * I'll say no for now. Smack does not do the frequent |
| 1361 | * label changing that SELinux does. |
| 1362 | */ |
| 1363 | static int smack_file_permission(struct file *file, int mask) |
| 1364 | { |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * smack_file_alloc_security - assign a file security blob |
| 1370 | * @file: the object |
| 1371 | * |
| 1372 | * The security blob for a file is a pointer to the master |
| 1373 | * label list, so no allocation is done. |
| 1374 | * |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1375 | * f_security is the owner security information. It |
| 1376 | * isn't used on file access checks, it's for send_sigio. |
| 1377 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1378 | * Returns 0 |
| 1379 | */ |
| 1380 | static int smack_file_alloc_security(struct file *file) |
| 1381 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1382 | struct smack_known *skp = smk_of_current(); |
| 1383 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1384 | file->f_security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | /** |
| 1389 | * smack_file_free_security - clear a file security blob |
| 1390 | * @file: the object |
| 1391 | * |
| 1392 | * The security blob for a file is a pointer to the master |
| 1393 | * label list, so no memory is freed. |
| 1394 | */ |
| 1395 | static void smack_file_free_security(struct file *file) |
| 1396 | { |
| 1397 | file->f_security = NULL; |
| 1398 | } |
| 1399 | |
| 1400 | /** |
| 1401 | * smack_file_ioctl - Smack check on ioctls |
| 1402 | * @file: the object |
| 1403 | * @cmd: what to do |
| 1404 | * @arg: unused |
| 1405 | * |
| 1406 | * Relies heavily on the correct use of the ioctl command conventions. |
| 1407 | * |
| 1408 | * Returns 0 if allowed, error code otherwise |
| 1409 | */ |
| 1410 | static int smack_file_ioctl(struct file *file, unsigned int cmd, |
| 1411 | unsigned long arg) |
| 1412 | { |
| 1413 | int rc = 0; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1414 | struct smk_audit_info ad; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1415 | struct inode *inode = file_inode(file); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1416 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1417 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1418 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1419 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1420 | if (_IOC_DIR(cmd) & _IOC_WRITE) { |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1421 | rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1422 | rc = smk_bu_file(file, MAY_WRITE, rc); |
| 1423 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1424 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1425 | if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) { |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1426 | rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1427 | rc = smk_bu_file(file, MAY_READ, rc); |
| 1428 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1429 | |
| 1430 | return rc; |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * smack_file_lock - Smack check on file locking |
| 1435 | * @file: the object |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1436 | * @cmd: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1437 | * |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1438 | * Returns 0 if current has lock access, error code otherwise |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1439 | */ |
| 1440 | static int smack_file_lock(struct file *file, unsigned int cmd) |
| 1441 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1442 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1443 | int rc; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1444 | struct inode *inode = file_inode(file); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1445 | |
Eric Paris | 92f4250 | 2011-04-25 13:15:55 -0400 | [diff] [blame] | 1446 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1447 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1448 | rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1449 | rc = smk_bu_file(file, MAY_LOCK, rc); |
| 1450 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /** |
| 1454 | * smack_file_fcntl - Smack check on fcntl |
| 1455 | * @file: the object |
| 1456 | * @cmd: what action to check |
| 1457 | * @arg: unused |
| 1458 | * |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1459 | * Generally these operations are harmless. |
| 1460 | * File locking operations present an obvious mechanism |
| 1461 | * for passing information, so they require write access. |
| 1462 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1463 | * Returns 0 if current has access, error code otherwise |
| 1464 | */ |
| 1465 | static int smack_file_fcntl(struct file *file, unsigned int cmd, |
| 1466 | unsigned long arg) |
| 1467 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1468 | struct smk_audit_info ad; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1469 | int rc = 0; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1470 | struct inode *inode = file_inode(file); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1471 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1472 | switch (cmd) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1473 | case F_GETLK: |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1474 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1475 | case F_SETLK: |
| 1476 | case F_SETLKW: |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1477 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1478 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1479 | rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1480 | rc = smk_bu_file(file, MAY_LOCK, rc); |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1481 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1482 | case F_SETOWN: |
| 1483 | case F_SETSIG: |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1484 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1485 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1486 | rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1487 | rc = smk_bu_file(file, MAY_WRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1488 | break; |
| 1489 | default: |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1490 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | return rc; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 1497 | * smack_mmap_file : |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1498 | * Check permissions for a mmap operation. The @file may be NULL, e.g. |
| 1499 | * if mapping anonymous memory. |
| 1500 | * @file contains the file structure for file to map (may be NULL). |
| 1501 | * @reqprot contains the protection requested by the application. |
| 1502 | * @prot contains the protection that will be applied by the kernel. |
| 1503 | * @flags contains the operational flags. |
| 1504 | * Return 0 if permission is granted. |
| 1505 | */ |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 1506 | static int smack_mmap_file(struct file *file, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1507 | unsigned long reqprot, unsigned long prot, |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 1508 | unsigned long flags) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1509 | { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 1510 | struct smack_known *skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1511 | struct smack_known *mkp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1512 | struct smack_rule *srp; |
| 1513 | struct task_smack *tsp; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1514 | struct smack_known *okp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1515 | struct inode_smack *isp; |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1516 | int may; |
| 1517 | int mmay; |
| 1518 | int tmay; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1519 | int rc; |
| 1520 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1521 | if (file == NULL) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1522 | return 0; |
| 1523 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1524 | isp = file_inode(file)->i_security; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1525 | if (isp->smk_mmap == NULL) |
| 1526 | return 0; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1527 | mkp = isp->smk_mmap; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1528 | |
| 1529 | tsp = current_security(); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1530 | skp = smk_of_current(); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1531 | rc = 0; |
| 1532 | |
| 1533 | rcu_read_lock(); |
| 1534 | /* |
| 1535 | * For each Smack rule associated with the subject |
| 1536 | * label verify that the SMACK64MMAP also has access |
| 1537 | * to that rule's object label. |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1538 | */ |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 1539 | list_for_each_entry_rcu(srp, &skp->smk_rules, list) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1540 | okp = srp->smk_object; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1541 | /* |
| 1542 | * Matching labels always allows access. |
| 1543 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1544 | if (mkp->smk_known == okp->smk_known) |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1545 | continue; |
| 1546 | /* |
| 1547 | * If there is a matching local rule take |
| 1548 | * that into account as well. |
| 1549 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1550 | may = smk_access_entry(srp->smk_subject->smk_known, |
| 1551 | okp->smk_known, |
| 1552 | &tsp->smk_rules); |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1553 | if (may == -ENOENT) |
| 1554 | may = srp->smk_access; |
| 1555 | else |
| 1556 | may &= srp->smk_access; |
| 1557 | /* |
| 1558 | * If may is zero the SMACK64MMAP subject can't |
| 1559 | * possibly have less access. |
| 1560 | */ |
| 1561 | if (may == 0) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1562 | continue; |
| 1563 | |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1564 | /* |
| 1565 | * Fetch the global list entry. |
| 1566 | * If there isn't one a SMACK64MMAP subject |
| 1567 | * can't have as much access as current. |
| 1568 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1569 | mmay = smk_access_entry(mkp->smk_known, okp->smk_known, |
| 1570 | &mkp->smk_rules); |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1571 | if (mmay == -ENOENT) { |
| 1572 | rc = -EACCES; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1573 | break; |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1574 | } |
| 1575 | /* |
| 1576 | * If there is a local entry it modifies the |
| 1577 | * potential access, too. |
| 1578 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1579 | tmay = smk_access_entry(mkp->smk_known, okp->smk_known, |
| 1580 | &tsp->smk_rules); |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1581 | if (tmay != -ENOENT) |
| 1582 | mmay &= tmay; |
| 1583 | |
| 1584 | /* |
| 1585 | * If there is any access available to current that is |
| 1586 | * not available to a SMACK64MMAP subject |
| 1587 | * deny access. |
| 1588 | */ |
Casey Schaufler | 75a2563 | 2011-02-09 19:58:42 -0800 | [diff] [blame] | 1589 | if ((may | mmay) != mmay) { |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1590 | rc = -EACCES; |
| 1591 | break; |
| 1592 | } |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | rcu_read_unlock(); |
| 1596 | |
| 1597 | return rc; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1601 | * smack_file_set_fowner - set the file security blob value |
| 1602 | * @file: object in question |
| 1603 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1604 | */ |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 1605 | static void smack_file_set_fowner(struct file *file) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1606 | { |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1607 | file->f_security = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /** |
| 1611 | * smack_file_send_sigiotask - Smack on sigio |
| 1612 | * @tsk: The target task |
| 1613 | * @fown: the object the signal come from |
| 1614 | * @signum: unused |
| 1615 | * |
| 1616 | * Allow a privileged task to get signals even if it shouldn't |
| 1617 | * |
| 1618 | * Returns 0 if a subject with the object's smack could |
| 1619 | * write to the task, an error code otherwise. |
| 1620 | */ |
| 1621 | static int smack_file_send_sigiotask(struct task_struct *tsk, |
| 1622 | struct fown_struct *fown, int signum) |
| 1623 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1624 | struct smack_known *skp; |
| 1625 | struct smack_known *tkp = smk_of_task(tsk->cred->security); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1626 | struct file *file; |
| 1627 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1628 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1629 | |
| 1630 | /* |
| 1631 | * struct fown_struct is never outside the context of a struct file |
| 1632 | */ |
| 1633 | file = container_of(fown, struct file, f_owner); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1634 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1635 | /* we don't log here as rc can be overriden */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1636 | skp = file->f_security; |
| 1637 | rc = smk_access(skp, tkp, MAY_WRITE, NULL); |
| 1638 | rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 1639 | if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE)) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1640 | rc = 0; |
| 1641 | |
| 1642 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); |
| 1643 | smk_ad_setfield_u_tsk(&ad, tsk); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1644 | smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1645 | return rc; |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * smack_file_receive - Smack file receive check |
| 1650 | * @file: the object |
| 1651 | * |
| 1652 | * Returns 0 if current has access, error code otherwise |
| 1653 | */ |
| 1654 | static int smack_file_receive(struct file *file) |
| 1655 | { |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1656 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1657 | int may = 0; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1658 | struct smk_audit_info ad; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1659 | struct inode *inode = file_inode(file); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1660 | |
Casey Schaufler | 4482a44 | 2013-12-30 17:37:45 -0800 | [diff] [blame] | 1661 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1662 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1663 | /* |
| 1664 | * This code relies on bitmasks. |
| 1665 | */ |
| 1666 | if (file->f_mode & FMODE_READ) |
| 1667 | may = MAY_READ; |
| 1668 | if (file->f_mode & FMODE_WRITE) |
| 1669 | may |= MAY_WRITE; |
| 1670 | |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1671 | rc = smk_curacc(smk_of_inode(inode), may, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1672 | rc = smk_bu_file(file, may, rc); |
| 1673 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1676 | /** |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 1677 | * smack_file_open - Smack dentry open processing |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1678 | * @file: the object |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1679 | * @cred: task credential |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1680 | * |
| 1681 | * Set the security blob in the file structure. |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1682 | * Allow the open only if the task has read access. There are |
| 1683 | * many read operations (e.g. fstat) that you can do with an |
| 1684 | * fd even if you have the file open write-only. |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1685 | * |
| 1686 | * Returns 0 |
| 1687 | */ |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 1688 | static int smack_file_open(struct file *file, const struct cred *cred) |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1689 | { |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1690 | struct task_smack *tsp = cred->security; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1691 | struct inode *inode = file_inode(file); |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1692 | struct smk_audit_info ad; |
| 1693 | int rc; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1694 | |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1695 | if (smack_privileged(CAP_MAC_OVERRIDE)) |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1696 | return 0; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1697 | |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1698 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1699 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1700 | rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1701 | rc = smk_bu_credfile(cred, file, MAY_READ, rc); |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1702 | |
| 1703 | return rc; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1706 | /* |
| 1707 | * Task hooks |
| 1708 | */ |
| 1709 | |
| 1710 | /** |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1711 | * smack_cred_alloc_blank - "allocate" blank task-level security credentials |
| 1712 | * @new: the new credentials |
| 1713 | * @gfp: the atomicity of any memory allocations |
| 1714 | * |
| 1715 | * Prepare a blank set of credentials for modification. This must allocate all |
| 1716 | * the memory the LSM module might require such that cred_transfer() can |
| 1717 | * complete without error. |
| 1718 | */ |
| 1719 | static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 1720 | { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1721 | struct task_smack *tsp; |
| 1722 | |
| 1723 | tsp = new_task_smack(NULL, NULL, gfp); |
| 1724 | if (tsp == NULL) |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1725 | return -ENOMEM; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1726 | |
| 1727 | cred->security = tsp; |
| 1728 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | |
| 1733 | /** |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 1734 | * smack_cred_free - "free" task-level security credentials |
| 1735 | * @cred: the credentials in question |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1736 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1737 | */ |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 1738 | static void smack_cred_free(struct cred *cred) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1739 | { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1740 | struct task_smack *tsp = cred->security; |
| 1741 | struct smack_rule *rp; |
| 1742 | struct list_head *l; |
| 1743 | struct list_head *n; |
| 1744 | |
| 1745 | if (tsp == NULL) |
| 1746 | return; |
| 1747 | cred->security = NULL; |
| 1748 | |
| 1749 | list_for_each_safe(l, n, &tsp->smk_rules) { |
| 1750 | rp = list_entry(l, struct smack_rule, list); |
| 1751 | list_del(&rp->list); |
| 1752 | kfree(rp); |
| 1753 | } |
| 1754 | kfree(tsp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | /** |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1758 | * smack_cred_prepare - prepare new set of credentials for modification |
| 1759 | * @new: the new credentials |
| 1760 | * @old: the original credentials |
| 1761 | * @gfp: the atomicity of any memory allocations |
| 1762 | * |
| 1763 | * Prepare a new set of credentials for modification. |
| 1764 | */ |
| 1765 | static int smack_cred_prepare(struct cred *new, const struct cred *old, |
| 1766 | gfp_t gfp) |
| 1767 | { |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1768 | struct task_smack *old_tsp = old->security; |
| 1769 | struct task_smack *new_tsp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1770 | int rc; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1771 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1772 | new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp); |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1773 | if (new_tsp == NULL) |
| 1774 | return -ENOMEM; |
| 1775 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1776 | rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp); |
| 1777 | if (rc != 0) |
| 1778 | return rc; |
| 1779 | |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1780 | new->security = new_tsp; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1781 | return 0; |
| 1782 | } |
| 1783 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1784 | /** |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1785 | * smack_cred_transfer - Transfer the old credentials to the new credentials |
| 1786 | * @new: the new credentials |
| 1787 | * @old: the original credentials |
| 1788 | * |
| 1789 | * Fill in a set of blank credentials from another set of credentials. |
| 1790 | */ |
| 1791 | static void smack_cred_transfer(struct cred *new, const struct cred *old) |
| 1792 | { |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1793 | struct task_smack *old_tsp = old->security; |
| 1794 | struct task_smack *new_tsp = new->security; |
| 1795 | |
| 1796 | new_tsp->smk_task = old_tsp->smk_task; |
| 1797 | new_tsp->smk_forked = old_tsp->smk_task; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1798 | mutex_init(&new_tsp->smk_rules_lock); |
| 1799 | INIT_LIST_HEAD(&new_tsp->smk_rules); |
| 1800 | |
| 1801 | |
| 1802 | /* cbs copy rule list */ |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | /** |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1806 | * smack_kernel_act_as - Set the subjective context in a set of credentials |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1807 | * @new: points to the set of credentials to be modified. |
| 1808 | * @secid: specifies the security ID to be set |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1809 | * |
| 1810 | * Set the security data for a kernel service. |
| 1811 | */ |
| 1812 | static int smack_kernel_act_as(struct cred *new, u32 secid) |
| 1813 | { |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1814 | struct task_smack *new_tsp = new->security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1815 | struct smack_known *skp = smack_from_secid(secid); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1816 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1817 | if (skp == NULL) |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1818 | return -EINVAL; |
| 1819 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1820 | new_tsp->smk_task = skp; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | /** |
| 1825 | * smack_kernel_create_files_as - Set the file creation label in a set of creds |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1826 | * @new: points to the set of credentials to be modified |
| 1827 | * @inode: points to the inode to use as a reference |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1828 | * |
| 1829 | * Set the file creation context in a set of credentials to the same |
| 1830 | * as the objective context of the specified inode |
| 1831 | */ |
| 1832 | static int smack_kernel_create_files_as(struct cred *new, |
| 1833 | struct inode *inode) |
| 1834 | { |
| 1835 | struct inode_smack *isp = inode->i_security; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1836 | struct task_smack *tsp = new->security; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1837 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1838 | tsp->smk_forked = isp->smk_inode; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1839 | tsp->smk_task = tsp->smk_forked; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1840 | return 0; |
| 1841 | } |
| 1842 | |
| 1843 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1844 | * smk_curacc_on_task - helper to log task related access |
| 1845 | * @p: the task object |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1846 | * @access: the access requested |
| 1847 | * @caller: name of the calling function for audit |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1848 | * |
| 1849 | * Return 0 if access is permitted |
| 1850 | */ |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1851 | static int smk_curacc_on_task(struct task_struct *p, int access, |
| 1852 | const char *caller) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1853 | { |
| 1854 | struct smk_audit_info ad; |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 1855 | struct smack_known *skp = smk_of_task_struct(p); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1856 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1857 | |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1858 | smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1859 | smk_ad_setfield_u_tsk(&ad, p); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1860 | rc = smk_curacc(skp, access, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1861 | rc = smk_bu_task(p, access, rc); |
| 1862 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1866 | * smack_task_setpgid - Smack check on setting pgid |
| 1867 | * @p: the task object |
| 1868 | * @pgid: unused |
| 1869 | * |
| 1870 | * Return 0 if write access is permitted |
| 1871 | */ |
| 1872 | static int smack_task_setpgid(struct task_struct *p, pid_t pgid) |
| 1873 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1874 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * smack_task_getpgid - Smack access check for getpgid |
| 1879 | * @p: the object task |
| 1880 | * |
| 1881 | * Returns 0 if current can read the object task, error code otherwise |
| 1882 | */ |
| 1883 | static int smack_task_getpgid(struct task_struct *p) |
| 1884 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1885 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | /** |
| 1889 | * smack_task_getsid - Smack access check for getsid |
| 1890 | * @p: the object task |
| 1891 | * |
| 1892 | * Returns 0 if current can read the object task, error code otherwise |
| 1893 | */ |
| 1894 | static int smack_task_getsid(struct task_struct *p) |
| 1895 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1896 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | /** |
| 1900 | * smack_task_getsecid - get the secid of the task |
| 1901 | * @p: the object task |
| 1902 | * @secid: where to put the result |
| 1903 | * |
| 1904 | * Sets the secid to contain a u32 version of the smack label. |
| 1905 | */ |
| 1906 | static void smack_task_getsecid(struct task_struct *p, u32 *secid) |
| 1907 | { |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 1908 | struct smack_known *skp = smk_of_task_struct(p); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1909 | |
| 1910 | *secid = skp->smk_secid; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * smack_task_setnice - Smack check on setting nice |
| 1915 | * @p: the task object |
| 1916 | * @nice: unused |
| 1917 | * |
| 1918 | * Return 0 if write access is permitted |
| 1919 | */ |
| 1920 | static int smack_task_setnice(struct task_struct *p, int nice) |
| 1921 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 1922 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | /** |
| 1926 | * smack_task_setioprio - Smack check on setting ioprio |
| 1927 | * @p: the task object |
| 1928 | * @ioprio: unused |
| 1929 | * |
| 1930 | * Return 0 if write access is permitted |
| 1931 | */ |
| 1932 | static int smack_task_setioprio(struct task_struct *p, int ioprio) |
| 1933 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 1934 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | /** |
| 1938 | * smack_task_getioprio - Smack check on reading ioprio |
| 1939 | * @p: the task object |
| 1940 | * |
| 1941 | * Return 0 if read access is permitted |
| 1942 | */ |
| 1943 | static int smack_task_getioprio(struct task_struct *p) |
| 1944 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1945 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /** |
| 1949 | * smack_task_setscheduler - Smack check on setting scheduler |
| 1950 | * @p: the task object |
| 1951 | * @policy: unused |
| 1952 | * @lp: unused |
| 1953 | * |
| 1954 | * Return 0 if read access is permitted |
| 1955 | */ |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 1956 | static int smack_task_setscheduler(struct task_struct *p) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1957 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 1958 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /** |
| 1962 | * smack_task_getscheduler - Smack check on reading scheduler |
| 1963 | * @p: the task object |
| 1964 | * |
| 1965 | * Return 0 if read access is permitted |
| 1966 | */ |
| 1967 | static int smack_task_getscheduler(struct task_struct *p) |
| 1968 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1969 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /** |
| 1973 | * smack_task_movememory - Smack check on moving memory |
| 1974 | * @p: the task object |
| 1975 | * |
| 1976 | * Return 0 if write access is permitted |
| 1977 | */ |
| 1978 | static int smack_task_movememory(struct task_struct *p) |
| 1979 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1980 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /** |
| 1984 | * smack_task_kill - Smack check on signal delivery |
| 1985 | * @p: the task object |
| 1986 | * @info: unused |
| 1987 | * @sig: unused |
| 1988 | * @secid: identifies the smack to use in lieu of current's |
| 1989 | * |
| 1990 | * Return 0 if write access is permitted |
| 1991 | * |
| 1992 | * The secid behavior is an artifact of an SELinux hack |
| 1993 | * in the USB code. Someday it may go away. |
| 1994 | */ |
| 1995 | static int smack_task_kill(struct task_struct *p, struct siginfo *info, |
| 1996 | int sig, u32 secid) |
| 1997 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1998 | struct smk_audit_info ad; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1999 | struct smack_known *skp; |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 2000 | struct smack_known *tkp = smk_of_task_struct(p); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2001 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2002 | |
| 2003 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); |
| 2004 | smk_ad_setfield_u_tsk(&ad, p); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2005 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2006 | * Sending a signal requires that the sender |
| 2007 | * can write the receiver. |
| 2008 | */ |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2009 | if (secid == 0) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2010 | rc = smk_curacc(tkp, MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2011 | rc = smk_bu_task(p, MAY_WRITE, rc); |
| 2012 | return rc; |
| 2013 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2014 | /* |
| 2015 | * If the secid isn't 0 we're dealing with some USB IO |
| 2016 | * specific behavior. This is not clean. For one thing |
| 2017 | * we can't take privilege into account. |
| 2018 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2019 | skp = smack_from_secid(secid); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2020 | rc = smk_access(skp, tkp, MAY_WRITE, &ad); |
| 2021 | rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2022 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * smack_task_wait - Smack access check for waiting |
| 2027 | * @p: task to wait for |
| 2028 | * |
Casey Schaufler | c00bedb | 2012-08-09 17:46:38 -0700 | [diff] [blame] | 2029 | * Returns 0 |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2030 | */ |
| 2031 | static int smack_task_wait(struct task_struct *p) |
| 2032 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2033 | /* |
Casey Schaufler | c00bedb | 2012-08-09 17:46:38 -0700 | [diff] [blame] | 2034 | * Allow the operation to succeed. |
| 2035 | * Zombies are bad. |
| 2036 | * In userless environments (e.g. phones) programs |
| 2037 | * get marked with SMACK64EXEC and even if the parent |
| 2038 | * and child shouldn't be talking the parent still |
| 2039 | * may expect to know when the child exits. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2040 | */ |
Casey Schaufler | c00bedb | 2012-08-09 17:46:38 -0700 | [diff] [blame] | 2041 | return 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | /** |
| 2045 | * smack_task_to_inode - copy task smack into the inode blob |
| 2046 | * @p: task to copy from |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 2047 | * @inode: inode to copy to |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2048 | * |
| 2049 | * Sets the smack pointer in the inode security blob |
| 2050 | */ |
| 2051 | static void smack_task_to_inode(struct task_struct *p, struct inode *inode) |
| 2052 | { |
| 2053 | struct inode_smack *isp = inode->i_security; |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 2054 | struct smack_known *skp = smk_of_task_struct(p); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2055 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2056 | isp->smk_inode = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * Socket hooks. |
| 2061 | */ |
| 2062 | |
| 2063 | /** |
| 2064 | * smack_sk_alloc_security - Allocate a socket blob |
| 2065 | * @sk: the socket |
| 2066 | * @family: unused |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 2067 | * @gfp_flags: memory allocation flags |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2068 | * |
| 2069 | * Assign Smack pointers to current |
| 2070 | * |
| 2071 | * Returns 0 on success, -ENOMEM is there's no memory |
| 2072 | */ |
| 2073 | static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags) |
| 2074 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2075 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2076 | struct socket_smack *ssp; |
| 2077 | |
| 2078 | ssp = kzalloc(sizeof(struct socket_smack), gfp_flags); |
| 2079 | if (ssp == NULL) |
| 2080 | return -ENOMEM; |
| 2081 | |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 2082 | ssp->smk_in = skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2083 | ssp->smk_out = skp; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 2084 | ssp->smk_packet = NULL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2085 | |
| 2086 | sk->sk_security = ssp; |
| 2087 | |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | /** |
| 2092 | * smack_sk_free_security - Free a socket blob |
| 2093 | * @sk: the socket |
| 2094 | * |
| 2095 | * Clears the blob pointer |
| 2096 | */ |
| 2097 | static void smack_sk_free_security(struct sock *sk) |
| 2098 | { |
| 2099 | kfree(sk->sk_security); |
| 2100 | } |
| 2101 | |
| 2102 | /** |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2103 | * smack_host_label - check host based restrictions |
| 2104 | * @sip: the object end |
| 2105 | * |
| 2106 | * looks for host based access restrictions |
| 2107 | * |
| 2108 | * This version will only be appropriate for really small sets of single label |
| 2109 | * hosts. The caller is responsible for ensuring that the RCU read lock is |
| 2110 | * taken before calling this function. |
| 2111 | * |
| 2112 | * Returns the label of the far end or NULL if it's not special. |
| 2113 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2114 | static struct smack_known *smack_host_label(struct sockaddr_in *sip) |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2115 | { |
| 2116 | struct smk_netlbladdr *snp; |
| 2117 | struct in_addr *siap = &sip->sin_addr; |
| 2118 | |
| 2119 | if (siap->s_addr == 0) |
| 2120 | return NULL; |
| 2121 | |
| 2122 | list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) |
| 2123 | /* |
| 2124 | * we break after finding the first match because |
| 2125 | * the list is sorted from longest to shortest mask |
| 2126 | * so we have found the most specific match |
| 2127 | */ |
| 2128 | if ((&snp->smk_host.sin_addr)->s_addr == |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 2129 | (siap->s_addr & (&snp->smk_mask)->s_addr)) { |
| 2130 | /* we have found the special CIPSO option */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2131 | if (snp->smk_label == &smack_cipso_option) |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 2132 | return NULL; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2133 | return snp->smk_label; |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 2134 | } |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2135 | |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | |
| 2139 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2140 | * smack_netlabel - Set the secattr on a socket |
| 2141 | * @sk: the socket |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2142 | * @labeled: socket label scheme |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2143 | * |
| 2144 | * Convert the outbound smack value (smk_out) to a |
| 2145 | * secattr and attach it to the socket. |
| 2146 | * |
| 2147 | * Returns 0 on success or an error code |
| 2148 | */ |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2149 | static int smack_netlabel(struct sock *sk, int labeled) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2150 | { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2151 | struct smack_known *skp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2152 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2153 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2154 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2155 | /* |
| 2156 | * Usually the netlabel code will handle changing the |
| 2157 | * packet labeling based on the label. |
| 2158 | * The case of a single label host is different, because |
| 2159 | * a single label host should never get a labeled packet |
| 2160 | * even though the label is usually associated with a packet |
| 2161 | * label. |
| 2162 | */ |
| 2163 | local_bh_disable(); |
| 2164 | bh_lock_sock_nested(sk); |
| 2165 | |
| 2166 | if (ssp->smk_out == smack_net_ambient || |
| 2167 | labeled == SMACK_UNLABELED_SOCKET) |
| 2168 | netlbl_sock_delattr(sk); |
| 2169 | else { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2170 | skp = ssp->smk_out; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2171 | rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | bh_unlock_sock(sk); |
| 2175 | local_bh_enable(); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2176 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2177 | return rc; |
| 2178 | } |
| 2179 | |
| 2180 | /** |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2181 | * smack_netlbel_send - Set the secattr on a socket and perform access checks |
| 2182 | * @sk: the socket |
| 2183 | * @sap: the destination address |
| 2184 | * |
| 2185 | * Set the correct secattr for the given socket based on the destination |
| 2186 | * address and perform any outbound access checks needed. |
| 2187 | * |
| 2188 | * Returns 0 on success or an error code. |
| 2189 | * |
| 2190 | */ |
| 2191 | static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap) |
| 2192 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2193 | struct smack_known *skp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2194 | int rc; |
| 2195 | int sk_lbl; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2196 | struct smack_known *hkp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2197 | struct socket_smack *ssp = sk->sk_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2198 | struct smk_audit_info ad; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2199 | |
| 2200 | rcu_read_lock(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2201 | hkp = smack_host_label(sap); |
| 2202 | if (hkp != NULL) { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2203 | #ifdef CONFIG_AUDIT |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 2204 | struct lsm_network_audit net; |
| 2205 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 2206 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 2207 | ad.a.u.net->family = sap->sin_family; |
| 2208 | ad.a.u.net->dport = sap->sin_port; |
| 2209 | ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2210 | #endif |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 2211 | sk_lbl = SMACK_UNLABELED_SOCKET; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2212 | skp = ssp->smk_out; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2213 | rc = smk_access(skp, hkp, MAY_WRITE, &ad); |
| 2214 | rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc); |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2215 | } else { |
| 2216 | sk_lbl = SMACK_CIPSO_SOCKET; |
| 2217 | rc = 0; |
| 2218 | } |
| 2219 | rcu_read_unlock(); |
| 2220 | if (rc != 0) |
| 2221 | return rc; |
| 2222 | |
| 2223 | return smack_netlabel(sk, sk_lbl); |
| 2224 | } |
| 2225 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2226 | #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER) |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2227 | /** |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2228 | * smk_ipv6_port_label - Smack port access table management |
| 2229 | * @sock: socket |
| 2230 | * @address: address |
| 2231 | * |
| 2232 | * Create or update the port list entry |
| 2233 | */ |
| 2234 | static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address) |
| 2235 | { |
| 2236 | struct sock *sk = sock->sk; |
| 2237 | struct sockaddr_in6 *addr6; |
| 2238 | struct socket_smack *ssp = sock->sk->sk_security; |
| 2239 | struct smk_port_label *spp; |
| 2240 | unsigned short port = 0; |
| 2241 | |
| 2242 | if (address == NULL) { |
| 2243 | /* |
| 2244 | * This operation is changing the Smack information |
| 2245 | * on the bound socket. Take the changes to the port |
| 2246 | * as well. |
| 2247 | */ |
| 2248 | list_for_each_entry(spp, &smk_ipv6_port_list, list) { |
| 2249 | if (sk != spp->smk_sock) |
| 2250 | continue; |
| 2251 | spp->smk_in = ssp->smk_in; |
| 2252 | spp->smk_out = ssp->smk_out; |
| 2253 | return; |
| 2254 | } |
| 2255 | /* |
| 2256 | * A NULL address is only used for updating existing |
| 2257 | * bound entries. If there isn't one, it's OK. |
| 2258 | */ |
| 2259 | return; |
| 2260 | } |
| 2261 | |
| 2262 | addr6 = (struct sockaddr_in6 *)address; |
| 2263 | port = ntohs(addr6->sin6_port); |
| 2264 | /* |
| 2265 | * This is a special case that is safely ignored. |
| 2266 | */ |
| 2267 | if (port == 0) |
| 2268 | return; |
| 2269 | |
| 2270 | /* |
| 2271 | * Look for an existing port list entry. |
| 2272 | * This is an indication that a port is getting reused. |
| 2273 | */ |
| 2274 | list_for_each_entry(spp, &smk_ipv6_port_list, list) { |
| 2275 | if (spp->smk_port != port) |
| 2276 | continue; |
| 2277 | spp->smk_port = port; |
| 2278 | spp->smk_sock = sk; |
| 2279 | spp->smk_in = ssp->smk_in; |
| 2280 | spp->smk_out = ssp->smk_out; |
| 2281 | return; |
| 2282 | } |
| 2283 | |
| 2284 | /* |
| 2285 | * A new port entry is required. |
| 2286 | */ |
| 2287 | spp = kzalloc(sizeof(*spp), GFP_KERNEL); |
| 2288 | if (spp == NULL) |
| 2289 | return; |
| 2290 | |
| 2291 | spp->smk_port = port; |
| 2292 | spp->smk_sock = sk; |
| 2293 | spp->smk_in = ssp->smk_in; |
| 2294 | spp->smk_out = ssp->smk_out; |
| 2295 | |
| 2296 | list_add(&spp->list, &smk_ipv6_port_list); |
| 2297 | return; |
| 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * smk_ipv6_port_check - check Smack port access |
| 2302 | * @sock: socket |
| 2303 | * @address: address |
| 2304 | * |
| 2305 | * Create or update the port list entry |
| 2306 | */ |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 2307 | static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address, |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2308 | int act) |
| 2309 | { |
| 2310 | __be16 *bep; |
| 2311 | __be32 *be32p; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2312 | struct smk_port_label *spp; |
| 2313 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2314 | struct smack_known *skp; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2315 | unsigned short port = 0; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2316 | struct smack_known *object; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2317 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2318 | int rc; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2319 | #ifdef CONFIG_AUDIT |
| 2320 | struct lsm_network_audit net; |
| 2321 | #endif |
| 2322 | |
| 2323 | if (act == SMK_RECEIVING) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2324 | skp = smack_net_ambient; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2325 | object = ssp->smk_in; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2326 | } else { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2327 | skp = ssp->smk_out; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2328 | object = smack_net_ambient; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | /* |
| 2332 | * Get the IP address and port from the address. |
| 2333 | */ |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 2334 | port = ntohs(address->sin6_port); |
| 2335 | bep = (__be16 *)(&address->sin6_addr); |
| 2336 | be32p = (__be32 *)(&address->sin6_addr); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2337 | |
| 2338 | /* |
| 2339 | * It's remote, so port lookup does no good. |
| 2340 | */ |
| 2341 | if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1) |
| 2342 | goto auditout; |
| 2343 | |
| 2344 | /* |
| 2345 | * It's local so the send check has to have passed. |
| 2346 | */ |
| 2347 | if (act == SMK_RECEIVING) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2348 | skp = &smack_known_web; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2349 | goto auditout; |
| 2350 | } |
| 2351 | |
| 2352 | list_for_each_entry(spp, &smk_ipv6_port_list, list) { |
| 2353 | if (spp->smk_port != port) |
| 2354 | continue; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2355 | object = spp->smk_in; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2356 | if (act == SMK_CONNECTING) |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 2357 | ssp->smk_packet = spp->smk_out; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2358 | break; |
| 2359 | } |
| 2360 | |
| 2361 | auditout: |
| 2362 | |
| 2363 | #ifdef CONFIG_AUDIT |
| 2364 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 2365 | ad.a.u.net->family = sk->sk_family; |
| 2366 | ad.a.u.net->dport = port; |
| 2367 | if (act == SMK_RECEIVING) |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 2368 | ad.a.u.net->v6info.saddr = address->sin6_addr; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2369 | else |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 2370 | ad.a.u.net->v6info.daddr = address->sin6_addr; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2371 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2372 | rc = smk_access(skp, object, MAY_WRITE, &ad); |
| 2373 | rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc); |
| 2374 | return rc; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2375 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2376 | #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2377 | |
| 2378 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2379 | * smack_inode_setsecurity - set smack xattrs |
| 2380 | * @inode: the object |
| 2381 | * @name: attribute name |
| 2382 | * @value: attribute value |
| 2383 | * @size: size of the attribute |
| 2384 | * @flags: unused |
| 2385 | * |
| 2386 | * Sets the named attribute in the appropriate blob |
| 2387 | * |
| 2388 | * Returns 0 on success, or an error code |
| 2389 | */ |
| 2390 | static int smack_inode_setsecurity(struct inode *inode, const char *name, |
| 2391 | const void *value, size_t size, int flags) |
| 2392 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2393 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2394 | struct inode_smack *nsp = inode->i_security; |
| 2395 | struct socket_smack *ssp; |
| 2396 | struct socket *sock; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2397 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2398 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2399 | if (value == NULL || size > SMK_LONGLABEL || size == 0) |
Pankaj Kumar | 5e9ab59 | 2013-12-13 15:12:22 +0530 | [diff] [blame] | 2400 | return -EINVAL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2401 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2402 | skp = smk_import_entry(value, size); |
| 2403 | if (skp == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2404 | return -EINVAL; |
| 2405 | |
| 2406 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2407 | nsp->smk_inode = skp; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 2408 | nsp->smk_flags |= SMK_INODE_INSTANT; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2409 | return 0; |
| 2410 | } |
| 2411 | /* |
| 2412 | * The rest of the Smack xattrs are only on sockets. |
| 2413 | */ |
| 2414 | if (inode->i_sb->s_magic != SOCKFS_MAGIC) |
| 2415 | return -EOPNOTSUPP; |
| 2416 | |
| 2417 | sock = SOCKET_I(inode); |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 2418 | if (sock == NULL || sock->sk == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2419 | return -EOPNOTSUPP; |
| 2420 | |
| 2421 | ssp = sock->sk->sk_security; |
| 2422 | |
| 2423 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 2424 | ssp->smk_in = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2425 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2426 | ssp->smk_out = skp; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2427 | if (sock->sk->sk_family == PF_INET) { |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 2428 | rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET); |
| 2429 | if (rc != 0) |
| 2430 | printk(KERN_WARNING |
| 2431 | "Smack: \"%s\" netlbl error %d.\n", |
| 2432 | __func__, -rc); |
| 2433 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2434 | } else |
| 2435 | return -EOPNOTSUPP; |
| 2436 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2437 | #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2438 | if (sock->sk->sk_family == PF_INET6) |
| 2439 | smk_ipv6_port_label(sock, NULL); |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2440 | #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2441 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | /** |
| 2446 | * smack_socket_post_create - finish socket setup |
| 2447 | * @sock: the socket |
| 2448 | * @family: protocol family |
| 2449 | * @type: unused |
| 2450 | * @protocol: unused |
| 2451 | * @kern: unused |
| 2452 | * |
| 2453 | * Sets the netlabel information on the socket |
| 2454 | * |
| 2455 | * Returns 0 on success, and error code otherwise |
| 2456 | */ |
| 2457 | static int smack_socket_post_create(struct socket *sock, int family, |
| 2458 | int type, int protocol, int kern) |
| 2459 | { |
Marcin Lis | 7412301 | 2015-01-22 15:40:33 +0100 | [diff] [blame] | 2460 | struct socket_smack *ssp; |
| 2461 | |
| 2462 | if (sock->sk == NULL) |
| 2463 | return 0; |
| 2464 | |
| 2465 | /* |
| 2466 | * Sockets created by kernel threads receive web label. |
| 2467 | */ |
| 2468 | if (unlikely(current->flags & PF_KTHREAD)) { |
| 2469 | ssp = sock->sk->sk_security; |
| 2470 | ssp->smk_in = &smack_known_web; |
| 2471 | ssp->smk_out = &smack_known_web; |
| 2472 | } |
| 2473 | |
| 2474 | if (family != PF_INET) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2475 | return 0; |
| 2476 | /* |
| 2477 | * Set the outbound netlbl. |
| 2478 | */ |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2479 | return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET); |
| 2480 | } |
| 2481 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2482 | #ifndef CONFIG_SECURITY_SMACK_NETFILTER |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2483 | /** |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2484 | * smack_socket_bind - record port binding information. |
| 2485 | * @sock: the socket |
| 2486 | * @address: the port address |
| 2487 | * @addrlen: size of the address |
| 2488 | * |
| 2489 | * Records the label bound to a port. |
| 2490 | * |
| 2491 | * Returns 0 |
| 2492 | */ |
| 2493 | static int smack_socket_bind(struct socket *sock, struct sockaddr *address, |
| 2494 | int addrlen) |
| 2495 | { |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2496 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2497 | if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) |
| 2498 | smk_ipv6_port_label(sock, address); |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2499 | #endif |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2500 | |
| 2501 | return 0; |
| 2502 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2503 | #endif /* !CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2504 | |
| 2505 | /** |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2506 | * smack_socket_connect - connect access check |
| 2507 | * @sock: the socket |
| 2508 | * @sap: the other end |
| 2509 | * @addrlen: size of sap |
| 2510 | * |
| 2511 | * Verifies that a connection may be possible |
| 2512 | * |
| 2513 | * Returns 0 on success, and error code otherwise |
| 2514 | */ |
| 2515 | static int smack_socket_connect(struct socket *sock, struct sockaddr *sap, |
| 2516 | int addrlen) |
| 2517 | { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2518 | int rc = 0; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2519 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2520 | if (sock->sk == NULL) |
| 2521 | return 0; |
| 2522 | |
| 2523 | switch (sock->sk->sk_family) { |
| 2524 | case PF_INET: |
| 2525 | if (addrlen < sizeof(struct sockaddr_in)) |
| 2526 | return -EINVAL; |
| 2527 | rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap); |
| 2528 | break; |
| 2529 | case PF_INET6: |
| 2530 | if (addrlen < sizeof(struct sockaddr_in6)) |
| 2531 | return -EINVAL; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2532 | #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER) |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 2533 | rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap, |
| 2534 | SMK_CONNECTING); |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 2535 | #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2536 | break; |
| 2537 | } |
| 2538 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * smack_flags_to_may - convert S_ to MAY_ values |
| 2543 | * @flags: the S_ value |
| 2544 | * |
| 2545 | * Returns the equivalent MAY_ value |
| 2546 | */ |
| 2547 | static int smack_flags_to_may(int flags) |
| 2548 | { |
| 2549 | int may = 0; |
| 2550 | |
| 2551 | if (flags & S_IRUGO) |
| 2552 | may |= MAY_READ; |
| 2553 | if (flags & S_IWUGO) |
| 2554 | may |= MAY_WRITE; |
| 2555 | if (flags & S_IXUGO) |
| 2556 | may |= MAY_EXEC; |
| 2557 | |
| 2558 | return may; |
| 2559 | } |
| 2560 | |
| 2561 | /** |
| 2562 | * smack_msg_msg_alloc_security - Set the security blob for msg_msg |
| 2563 | * @msg: the object |
| 2564 | * |
| 2565 | * Returns 0 |
| 2566 | */ |
| 2567 | static int smack_msg_msg_alloc_security(struct msg_msg *msg) |
| 2568 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2569 | struct smack_known *skp = smk_of_current(); |
| 2570 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2571 | msg->security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2572 | return 0; |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | * smack_msg_msg_free_security - Clear the security blob for msg_msg |
| 2577 | * @msg: the object |
| 2578 | * |
| 2579 | * Clears the blob pointer |
| 2580 | */ |
| 2581 | static void smack_msg_msg_free_security(struct msg_msg *msg) |
| 2582 | { |
| 2583 | msg->security = NULL; |
| 2584 | } |
| 2585 | |
| 2586 | /** |
| 2587 | * smack_of_shm - the smack pointer for the shm |
| 2588 | * @shp: the object |
| 2589 | * |
| 2590 | * Returns a pointer to the smack value |
| 2591 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2592 | static struct smack_known *smack_of_shm(struct shmid_kernel *shp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2593 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2594 | return (struct smack_known *)shp->shm_perm.security; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /** |
| 2598 | * smack_shm_alloc_security - Set the security blob for shm |
| 2599 | * @shp: the object |
| 2600 | * |
| 2601 | * Returns 0 |
| 2602 | */ |
| 2603 | static int smack_shm_alloc_security(struct shmid_kernel *shp) |
| 2604 | { |
| 2605 | struct kern_ipc_perm *isp = &shp->shm_perm; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2606 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2607 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2608 | isp->security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2609 | return 0; |
| 2610 | } |
| 2611 | |
| 2612 | /** |
| 2613 | * smack_shm_free_security - Clear the security blob for shm |
| 2614 | * @shp: the object |
| 2615 | * |
| 2616 | * Clears the blob pointer |
| 2617 | */ |
| 2618 | static void smack_shm_free_security(struct shmid_kernel *shp) |
| 2619 | { |
| 2620 | struct kern_ipc_perm *isp = &shp->shm_perm; |
| 2621 | |
| 2622 | isp->security = NULL; |
| 2623 | } |
| 2624 | |
| 2625 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2626 | * smk_curacc_shm : check if current has access on shm |
| 2627 | * @shp : the object |
| 2628 | * @access : access requested |
| 2629 | * |
| 2630 | * Returns 0 if current has the requested access, error code otherwise |
| 2631 | */ |
| 2632 | static int smk_curacc_shm(struct shmid_kernel *shp, int access) |
| 2633 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2634 | struct smack_known *ssp = smack_of_shm(shp); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2635 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2636 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2637 | |
| 2638 | #ifdef CONFIG_AUDIT |
| 2639 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
| 2640 | ad.a.u.ipc_id = shp->shm_perm.id; |
| 2641 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2642 | rc = smk_curacc(ssp, access, &ad); |
| 2643 | rc = smk_bu_current("shm", ssp, access, rc); |
| 2644 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2648 | * smack_shm_associate - Smack access check for shm |
| 2649 | * @shp: the object |
| 2650 | * @shmflg: access requested |
| 2651 | * |
| 2652 | * Returns 0 if current has the requested access, error code otherwise |
| 2653 | */ |
| 2654 | static int smack_shm_associate(struct shmid_kernel *shp, int shmflg) |
| 2655 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2656 | int may; |
| 2657 | |
| 2658 | may = smack_flags_to_may(shmflg); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2659 | return smk_curacc_shm(shp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | /** |
| 2663 | * smack_shm_shmctl - Smack access check for shm |
| 2664 | * @shp: the object |
| 2665 | * @cmd: what it wants to do |
| 2666 | * |
| 2667 | * Returns 0 if current has the requested access, error code otherwise |
| 2668 | */ |
| 2669 | static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd) |
| 2670 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2671 | int may; |
| 2672 | |
| 2673 | switch (cmd) { |
| 2674 | case IPC_STAT: |
| 2675 | case SHM_STAT: |
| 2676 | may = MAY_READ; |
| 2677 | break; |
| 2678 | case IPC_SET: |
| 2679 | case SHM_LOCK: |
| 2680 | case SHM_UNLOCK: |
| 2681 | case IPC_RMID: |
| 2682 | may = MAY_READWRITE; |
| 2683 | break; |
| 2684 | case IPC_INFO: |
| 2685 | case SHM_INFO: |
| 2686 | /* |
| 2687 | * System level information. |
| 2688 | */ |
| 2689 | return 0; |
| 2690 | default: |
| 2691 | return -EINVAL; |
| 2692 | } |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2693 | return smk_curacc_shm(shp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | /** |
| 2697 | * smack_shm_shmat - Smack access for shmat |
| 2698 | * @shp: the object |
| 2699 | * @shmaddr: unused |
| 2700 | * @shmflg: access requested |
| 2701 | * |
| 2702 | * Returns 0 if current has the requested access, error code otherwise |
| 2703 | */ |
| 2704 | static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, |
| 2705 | int shmflg) |
| 2706 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2707 | int may; |
| 2708 | |
| 2709 | may = smack_flags_to_may(shmflg); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2710 | return smk_curacc_shm(shp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | /** |
| 2714 | * smack_of_sem - the smack pointer for the sem |
| 2715 | * @sma: the object |
| 2716 | * |
| 2717 | * Returns a pointer to the smack value |
| 2718 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2719 | static struct smack_known *smack_of_sem(struct sem_array *sma) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2720 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2721 | return (struct smack_known *)sma->sem_perm.security; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | /** |
| 2725 | * smack_sem_alloc_security - Set the security blob for sem |
| 2726 | * @sma: the object |
| 2727 | * |
| 2728 | * Returns 0 |
| 2729 | */ |
| 2730 | static int smack_sem_alloc_security(struct sem_array *sma) |
| 2731 | { |
| 2732 | struct kern_ipc_perm *isp = &sma->sem_perm; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2733 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2734 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2735 | isp->security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2736 | return 0; |
| 2737 | } |
| 2738 | |
| 2739 | /** |
| 2740 | * smack_sem_free_security - Clear the security blob for sem |
| 2741 | * @sma: the object |
| 2742 | * |
| 2743 | * Clears the blob pointer |
| 2744 | */ |
| 2745 | static void smack_sem_free_security(struct sem_array *sma) |
| 2746 | { |
| 2747 | struct kern_ipc_perm *isp = &sma->sem_perm; |
| 2748 | |
| 2749 | isp->security = NULL; |
| 2750 | } |
| 2751 | |
| 2752 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2753 | * smk_curacc_sem : check if current has access on sem |
| 2754 | * @sma : the object |
| 2755 | * @access : access requested |
| 2756 | * |
| 2757 | * Returns 0 if current has the requested access, error code otherwise |
| 2758 | */ |
| 2759 | static int smk_curacc_sem(struct sem_array *sma, int access) |
| 2760 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2761 | struct smack_known *ssp = smack_of_sem(sma); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2762 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2763 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2764 | |
| 2765 | #ifdef CONFIG_AUDIT |
| 2766 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
| 2767 | ad.a.u.ipc_id = sma->sem_perm.id; |
| 2768 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2769 | rc = smk_curacc(ssp, access, &ad); |
| 2770 | rc = smk_bu_current("sem", ssp, access, rc); |
| 2771 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2775 | * smack_sem_associate - Smack access check for sem |
| 2776 | * @sma: the object |
| 2777 | * @semflg: access requested |
| 2778 | * |
| 2779 | * Returns 0 if current has the requested access, error code otherwise |
| 2780 | */ |
| 2781 | static int smack_sem_associate(struct sem_array *sma, int semflg) |
| 2782 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2783 | int may; |
| 2784 | |
| 2785 | may = smack_flags_to_may(semflg); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2786 | return smk_curacc_sem(sma, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | /** |
| 2790 | * smack_sem_shmctl - Smack access check for sem |
| 2791 | * @sma: the object |
| 2792 | * @cmd: what it wants to do |
| 2793 | * |
| 2794 | * Returns 0 if current has the requested access, error code otherwise |
| 2795 | */ |
| 2796 | static int smack_sem_semctl(struct sem_array *sma, int cmd) |
| 2797 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2798 | int may; |
| 2799 | |
| 2800 | switch (cmd) { |
| 2801 | case GETPID: |
| 2802 | case GETNCNT: |
| 2803 | case GETZCNT: |
| 2804 | case GETVAL: |
| 2805 | case GETALL: |
| 2806 | case IPC_STAT: |
| 2807 | case SEM_STAT: |
| 2808 | may = MAY_READ; |
| 2809 | break; |
| 2810 | case SETVAL: |
| 2811 | case SETALL: |
| 2812 | case IPC_RMID: |
| 2813 | case IPC_SET: |
| 2814 | may = MAY_READWRITE; |
| 2815 | break; |
| 2816 | case IPC_INFO: |
| 2817 | case SEM_INFO: |
| 2818 | /* |
| 2819 | * System level information |
| 2820 | */ |
| 2821 | return 0; |
| 2822 | default: |
| 2823 | return -EINVAL; |
| 2824 | } |
| 2825 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2826 | return smk_curacc_sem(sma, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | /** |
| 2830 | * smack_sem_semop - Smack checks of semaphore operations |
| 2831 | * @sma: the object |
| 2832 | * @sops: unused |
| 2833 | * @nsops: unused |
| 2834 | * @alter: unused |
| 2835 | * |
| 2836 | * Treated as read and write in all cases. |
| 2837 | * |
| 2838 | * Returns 0 if access is allowed, error code otherwise |
| 2839 | */ |
| 2840 | static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops, |
| 2841 | unsigned nsops, int alter) |
| 2842 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2843 | return smk_curacc_sem(sma, MAY_READWRITE); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | /** |
| 2847 | * smack_msg_alloc_security - Set the security blob for msg |
| 2848 | * @msq: the object |
| 2849 | * |
| 2850 | * Returns 0 |
| 2851 | */ |
| 2852 | static int smack_msg_queue_alloc_security(struct msg_queue *msq) |
| 2853 | { |
| 2854 | struct kern_ipc_perm *kisp = &msq->q_perm; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2855 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2856 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2857 | kisp->security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2858 | return 0; |
| 2859 | } |
| 2860 | |
| 2861 | /** |
| 2862 | * smack_msg_free_security - Clear the security blob for msg |
| 2863 | * @msq: the object |
| 2864 | * |
| 2865 | * Clears the blob pointer |
| 2866 | */ |
| 2867 | static void smack_msg_queue_free_security(struct msg_queue *msq) |
| 2868 | { |
| 2869 | struct kern_ipc_perm *kisp = &msq->q_perm; |
| 2870 | |
| 2871 | kisp->security = NULL; |
| 2872 | } |
| 2873 | |
| 2874 | /** |
| 2875 | * smack_of_msq - the smack pointer for the msq |
| 2876 | * @msq: the object |
| 2877 | * |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2878 | * Returns a pointer to the smack label entry |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2879 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2880 | static struct smack_known *smack_of_msq(struct msg_queue *msq) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2881 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2882 | return (struct smack_known *)msq->q_perm.security; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2886 | * smk_curacc_msq : helper to check if current has access on msq |
| 2887 | * @msq : the msq |
| 2888 | * @access : access requested |
| 2889 | * |
| 2890 | * return 0 if current has access, error otherwise |
| 2891 | */ |
| 2892 | static int smk_curacc_msq(struct msg_queue *msq, int access) |
| 2893 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2894 | struct smack_known *msp = smack_of_msq(msq); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2895 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2896 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2897 | |
| 2898 | #ifdef CONFIG_AUDIT |
| 2899 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
| 2900 | ad.a.u.ipc_id = msq->q_perm.id; |
| 2901 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2902 | rc = smk_curacc(msp, access, &ad); |
| 2903 | rc = smk_bu_current("msq", msp, access, rc); |
| 2904 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2908 | * smack_msg_queue_associate - Smack access check for msg_queue |
| 2909 | * @msq: the object |
| 2910 | * @msqflg: access requested |
| 2911 | * |
| 2912 | * Returns 0 if current has the requested access, error code otherwise |
| 2913 | */ |
| 2914 | static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg) |
| 2915 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2916 | int may; |
| 2917 | |
| 2918 | may = smack_flags_to_may(msqflg); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2919 | return smk_curacc_msq(msq, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | /** |
| 2923 | * smack_msg_queue_msgctl - Smack access check for msg_queue |
| 2924 | * @msq: the object |
| 2925 | * @cmd: what it wants to do |
| 2926 | * |
| 2927 | * Returns 0 if current has the requested access, error code otherwise |
| 2928 | */ |
| 2929 | static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd) |
| 2930 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2931 | int may; |
| 2932 | |
| 2933 | switch (cmd) { |
| 2934 | case IPC_STAT: |
| 2935 | case MSG_STAT: |
| 2936 | may = MAY_READ; |
| 2937 | break; |
| 2938 | case IPC_SET: |
| 2939 | case IPC_RMID: |
| 2940 | may = MAY_READWRITE; |
| 2941 | break; |
| 2942 | case IPC_INFO: |
| 2943 | case MSG_INFO: |
| 2944 | /* |
| 2945 | * System level information |
| 2946 | */ |
| 2947 | return 0; |
| 2948 | default: |
| 2949 | return -EINVAL; |
| 2950 | } |
| 2951 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2952 | return smk_curacc_msq(msq, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | /** |
| 2956 | * smack_msg_queue_msgsnd - Smack access check for msg_queue |
| 2957 | * @msq: the object |
| 2958 | * @msg: unused |
| 2959 | * @msqflg: access requested |
| 2960 | * |
| 2961 | * Returns 0 if current has the requested access, error code otherwise |
| 2962 | */ |
| 2963 | static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, |
| 2964 | int msqflg) |
| 2965 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2966 | int may; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2967 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2968 | may = smack_flags_to_may(msqflg); |
| 2969 | return smk_curacc_msq(msq, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | /** |
| 2973 | * smack_msg_queue_msgsnd - Smack access check for msg_queue |
| 2974 | * @msq: the object |
| 2975 | * @msg: unused |
| 2976 | * @target: unused |
| 2977 | * @type: unused |
| 2978 | * @mode: unused |
| 2979 | * |
| 2980 | * Returns 0 if current has read and write access, error code otherwise |
| 2981 | */ |
| 2982 | static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, |
| 2983 | struct task_struct *target, long type, int mode) |
| 2984 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2985 | return smk_curacc_msq(msq, MAY_READWRITE); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /** |
| 2989 | * smack_ipc_permission - Smack access for ipc_permission() |
| 2990 | * @ipp: the object permissions |
| 2991 | * @flag: access requested |
| 2992 | * |
| 2993 | * Returns 0 if current has read and write access, error code otherwise |
| 2994 | */ |
| 2995 | static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) |
| 2996 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2997 | struct smack_known *iskp = ipp->security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2998 | int may = smack_flags_to_may(flag); |
| 2999 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3000 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3001 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3002 | #ifdef CONFIG_AUDIT |
| 3003 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
| 3004 | ad.a.u.ipc_id = ipp->id; |
| 3005 | #endif |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3006 | rc = smk_curacc(iskp, may, &ad); |
| 3007 | rc = smk_bu_current("svipc", iskp, may, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3008 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3009 | } |
| 3010 | |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3011 | /** |
| 3012 | * smack_ipc_getsecid - Extract smack security id |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3013 | * @ipp: the object permissions |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3014 | * @secid: where result will be saved |
| 3015 | */ |
| 3016 | static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid) |
| 3017 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3018 | struct smack_known *iskp = ipp->security; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3019 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3020 | *secid = iskp->smk_secid; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3021 | } |
| 3022 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3023 | /** |
| 3024 | * smack_d_instantiate - Make sure the blob is correct on an inode |
Dan Carpenter | 3e62cbb | 2010-06-01 09:14:04 +0200 | [diff] [blame] | 3025 | * @opt_dentry: dentry where inode will be attached |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3026 | * @inode: the object |
| 3027 | * |
| 3028 | * Set the inode's security blob if it hasn't been done already. |
| 3029 | */ |
| 3030 | static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) |
| 3031 | { |
| 3032 | struct super_block *sbp; |
| 3033 | struct superblock_smack *sbsp; |
| 3034 | struct inode_smack *isp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3035 | struct smack_known *skp; |
| 3036 | struct smack_known *ckp = smk_of_current(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3037 | struct smack_known *final; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3038 | char trattr[TRANS_TRUE_SIZE]; |
| 3039 | int transflag = 0; |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3040 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3041 | struct dentry *dp; |
| 3042 | |
| 3043 | if (inode == NULL) |
| 3044 | return; |
| 3045 | |
| 3046 | isp = inode->i_security; |
| 3047 | |
| 3048 | mutex_lock(&isp->smk_lock); |
| 3049 | /* |
| 3050 | * If the inode is already instantiated |
| 3051 | * take the quick way out |
| 3052 | */ |
| 3053 | if (isp->smk_flags & SMK_INODE_INSTANT) |
| 3054 | goto unlockandout; |
| 3055 | |
| 3056 | sbp = inode->i_sb; |
| 3057 | sbsp = sbp->s_security; |
| 3058 | /* |
| 3059 | * We're going to use the superblock default label |
| 3060 | * if there's no label on the file. |
| 3061 | */ |
| 3062 | final = sbsp->smk_default; |
| 3063 | |
| 3064 | /* |
Casey Schaufler | e97dcb0 | 2008-06-02 10:04:32 -0700 | [diff] [blame] | 3065 | * If this is the root inode the superblock |
| 3066 | * may be in the process of initialization. |
| 3067 | * If that is the case use the root value out |
| 3068 | * of the superblock. |
| 3069 | */ |
| 3070 | if (opt_dentry->d_parent == opt_dentry) { |
Łukasz Stelmach | 1d8c2326 | 2014-12-16 16:53:08 +0100 | [diff] [blame] | 3071 | switch (sbp->s_magic) { |
| 3072 | case CGROUP_SUPER_MAGIC: |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3073 | /* |
| 3074 | * The cgroup filesystem is never mounted, |
| 3075 | * so there's no opportunity to set the mount |
| 3076 | * options. |
| 3077 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3078 | sbsp->smk_root = &smack_known_star; |
| 3079 | sbsp->smk_default = &smack_known_star; |
Łukasz Stelmach | 1d8c2326 | 2014-12-16 16:53:08 +0100 | [diff] [blame] | 3080 | isp->smk_inode = sbsp->smk_root; |
| 3081 | break; |
| 3082 | case TMPFS_MAGIC: |
| 3083 | /* |
| 3084 | * What about shmem/tmpfs anonymous files with dentry |
| 3085 | * obtained from d_alloc_pseudo()? |
| 3086 | */ |
| 3087 | isp->smk_inode = smk_of_current(); |
| 3088 | break; |
| 3089 | default: |
| 3090 | isp->smk_inode = sbsp->smk_root; |
| 3091 | break; |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3092 | } |
Casey Schaufler | e97dcb0 | 2008-06-02 10:04:32 -0700 | [diff] [blame] | 3093 | isp->smk_flags |= SMK_INODE_INSTANT; |
| 3094 | goto unlockandout; |
| 3095 | } |
| 3096 | |
| 3097 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3098 | * This is pretty hackish. |
| 3099 | * Casey says that we shouldn't have to do |
| 3100 | * file system specific code, but it does help |
| 3101 | * with keeping it simple. |
| 3102 | */ |
| 3103 | switch (sbp->s_magic) { |
| 3104 | case SMACK_MAGIC: |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3105 | case PIPEFS_MAGIC: |
| 3106 | case SOCKFS_MAGIC: |
| 3107 | case CGROUP_SUPER_MAGIC: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3108 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3109 | * Casey says that it's a little embarrassing |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3110 | * that the smack file system doesn't do |
| 3111 | * extended attributes. |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3112 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3113 | * Casey says pipes are easy (?) |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3114 | * |
| 3115 | * Socket access is controlled by the socket |
| 3116 | * structures associated with the task involved. |
| 3117 | * |
| 3118 | * Cgroupfs is special |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3119 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3120 | final = &smack_known_star; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3121 | break; |
| 3122 | case DEVPTS_SUPER_MAGIC: |
| 3123 | /* |
| 3124 | * devpts seems content with the label of the task. |
| 3125 | * Programs that change smack have to treat the |
| 3126 | * pty with respect. |
| 3127 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3128 | final = ckp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3129 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3130 | case PROC_SUPER_MAGIC: |
| 3131 | /* |
| 3132 | * Casey says procfs appears not to care. |
| 3133 | * The superblock default suffices. |
| 3134 | */ |
| 3135 | break; |
| 3136 | case TMPFS_MAGIC: |
| 3137 | /* |
| 3138 | * Device labels should come from the filesystem, |
| 3139 | * but watch out, because they're volitile, |
| 3140 | * getting recreated on every reboot. |
| 3141 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3142 | final = &smack_known_star; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3143 | /* |
| 3144 | * No break. |
| 3145 | * |
| 3146 | * If a smack value has been set we want to use it, |
| 3147 | * but since tmpfs isn't giving us the opportunity |
| 3148 | * to set mount options simulate setting the |
| 3149 | * superblock default. |
| 3150 | */ |
| 3151 | default: |
| 3152 | /* |
| 3153 | * This isn't an understood special case. |
| 3154 | * Get the value from the xattr. |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3155 | */ |
| 3156 | |
| 3157 | /* |
| 3158 | * UNIX domain sockets use lower level socket data. |
| 3159 | */ |
| 3160 | if (S_ISSOCK(inode->i_mode)) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3161 | final = &smack_known_star; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3162 | break; |
| 3163 | } |
| 3164 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3165 | * No xattr support means, alas, no SMACK label. |
| 3166 | * Use the aforeapplied default. |
| 3167 | * It would be curious if the label of the task |
| 3168 | * does not match that assigned. |
| 3169 | */ |
| 3170 | if (inode->i_op->getxattr == NULL) |
| 3171 | break; |
| 3172 | /* |
| 3173 | * Get the dentry for xattr. |
| 3174 | */ |
Dan Carpenter | 3e62cbb | 2010-06-01 09:14:04 +0200 | [diff] [blame] | 3175 | dp = dget(opt_dentry); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3176 | skp = smk_fetch(XATTR_NAME_SMACK, inode, dp); |
| 3177 | if (skp != NULL) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3178 | final = skp; |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3179 | |
| 3180 | /* |
| 3181 | * Transmuting directory |
| 3182 | */ |
| 3183 | if (S_ISDIR(inode->i_mode)) { |
| 3184 | /* |
| 3185 | * If this is a new directory and the label was |
| 3186 | * transmuted when the inode was initialized |
| 3187 | * set the transmute attribute on the directory |
| 3188 | * and mark the inode. |
| 3189 | * |
| 3190 | * If there is a transmute attribute on the |
| 3191 | * directory mark the inode. |
| 3192 | */ |
| 3193 | if (isp->smk_flags & SMK_INODE_CHANGED) { |
| 3194 | isp->smk_flags &= ~SMK_INODE_CHANGED; |
| 3195 | rc = inode->i_op->setxattr(dp, |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3196 | XATTR_NAME_SMACKTRANSMUTE, |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3197 | TRANS_TRUE, TRANS_TRUE_SIZE, |
| 3198 | 0); |
| 3199 | } else { |
| 3200 | rc = inode->i_op->getxattr(dp, |
| 3201 | XATTR_NAME_SMACKTRANSMUTE, trattr, |
| 3202 | TRANS_TRUE_SIZE); |
| 3203 | if (rc >= 0 && strncmp(trattr, TRANS_TRUE, |
| 3204 | TRANS_TRUE_SIZE) != 0) |
| 3205 | rc = -EINVAL; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3206 | } |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3207 | if (rc >= 0) |
| 3208 | transflag = SMK_INODE_TRANSMUTE; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3209 | } |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 3210 | /* |
| 3211 | * Don't let the exec or mmap label be "*" or "@". |
| 3212 | */ |
| 3213 | skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp); |
| 3214 | if (skp == &smack_known_star || skp == &smack_known_web) |
| 3215 | skp = NULL; |
| 3216 | isp->smk_task = skp; |
| 3217 | skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp); |
| 3218 | if (skp == &smack_known_star || skp == &smack_known_web) |
| 3219 | skp = NULL; |
| 3220 | isp->smk_mmap = skp; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 3221 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3222 | dput(dp); |
| 3223 | break; |
| 3224 | } |
| 3225 | |
| 3226 | if (final == NULL) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3227 | isp->smk_inode = ckp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3228 | else |
| 3229 | isp->smk_inode = final; |
| 3230 | |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3231 | isp->smk_flags |= (SMK_INODE_INSTANT | transflag); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3232 | |
| 3233 | unlockandout: |
| 3234 | mutex_unlock(&isp->smk_lock); |
| 3235 | return; |
| 3236 | } |
| 3237 | |
| 3238 | /** |
| 3239 | * smack_getprocattr - Smack process attribute access |
| 3240 | * @p: the object task |
| 3241 | * @name: the name of the attribute in /proc/.../attr |
| 3242 | * @value: where to put the result |
| 3243 | * |
| 3244 | * Places a copy of the task Smack into value |
| 3245 | * |
| 3246 | * Returns the length of the smack label or an error code |
| 3247 | */ |
| 3248 | static int smack_getprocattr(struct task_struct *p, char *name, char **value) |
| 3249 | { |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 3250 | struct smack_known *skp = smk_of_task_struct(p); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3251 | char *cp; |
| 3252 | int slen; |
| 3253 | |
| 3254 | if (strcmp(name, "current") != 0) |
| 3255 | return -EINVAL; |
| 3256 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3257 | cp = kstrdup(skp->smk_known, GFP_KERNEL); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3258 | if (cp == NULL) |
| 3259 | return -ENOMEM; |
| 3260 | |
| 3261 | slen = strlen(cp); |
| 3262 | *value = cp; |
| 3263 | return slen; |
| 3264 | } |
| 3265 | |
| 3266 | /** |
| 3267 | * smack_setprocattr - Smack process attribute setting |
| 3268 | * @p: the object task |
| 3269 | * @name: the name of the attribute in /proc/.../attr |
| 3270 | * @value: the value to set |
| 3271 | * @size: the size of the value |
| 3272 | * |
| 3273 | * Sets the Smack value of the task. Only setting self |
| 3274 | * is permitted and only with privilege |
| 3275 | * |
| 3276 | * Returns the length of the smack label or an error code |
| 3277 | */ |
| 3278 | static int smack_setprocattr(struct task_struct *p, char *name, |
| 3279 | void *value, size_t size) |
| 3280 | { |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 3281 | struct task_smack *tsp; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3282 | struct cred *new; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3283 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3284 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3285 | /* |
| 3286 | * Changing another process' Smack value is too dangerous |
| 3287 | * and supports no sane use case. |
| 3288 | */ |
| 3289 | if (p != current) |
| 3290 | return -EPERM; |
| 3291 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 3292 | if (!smack_privileged(CAP_MAC_ADMIN)) |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 3293 | return -EPERM; |
| 3294 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3295 | if (value == NULL || size == 0 || size >= SMK_LONGLABEL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3296 | return -EINVAL; |
| 3297 | |
| 3298 | if (strcmp(name, "current") != 0) |
| 3299 | return -EINVAL; |
| 3300 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3301 | skp = smk_import_entry(value, size); |
| 3302 | if (skp == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3303 | return -EINVAL; |
| 3304 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3305 | /* |
| 3306 | * No process is ever allowed the web ("@") label. |
| 3307 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3308 | if (skp == &smack_known_web) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3309 | return -EPERM; |
| 3310 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3311 | new = prepare_creds(); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3312 | if (new == NULL) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3313 | return -ENOMEM; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 3314 | |
Casey Schaufler | 46a2f3b | 2012-08-22 11:44:03 -0700 | [diff] [blame] | 3315 | tsp = new->security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3316 | tsp->smk_task = skp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 3317 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3318 | commit_creds(new); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3319 | return size; |
| 3320 | } |
| 3321 | |
| 3322 | /** |
| 3323 | * smack_unix_stream_connect - Smack access on UDS |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 3324 | * @sock: one sock |
| 3325 | * @other: the other sock |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3326 | * @newsk: unused |
| 3327 | * |
| 3328 | * Return 0 if a subject with the smack of sock could access |
| 3329 | * an object with the smack of other, otherwise an error code |
| 3330 | */ |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 3331 | static int smack_unix_stream_connect(struct sock *sock, |
| 3332 | struct sock *other, struct sock *newsk) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3333 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3334 | struct smack_known *skp; |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3335 | struct smack_known *okp; |
James Morris | d2e7ad1 | 2011-01-10 09:46:24 +1100 | [diff] [blame] | 3336 | struct socket_smack *ssp = sock->sk_security; |
| 3337 | struct socket_smack *osp = other->sk_security; |
Casey Schaufler | 975d5e5 | 2011-09-26 14:43:39 -0700 | [diff] [blame] | 3338 | struct socket_smack *nsp = newsk->sk_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3339 | struct smk_audit_info ad; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3340 | int rc = 0; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3341 | #ifdef CONFIG_AUDIT |
| 3342 | struct lsm_network_audit net; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3343 | #endif |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3344 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3345 | if (!smack_privileged(CAP_MAC_OVERRIDE)) { |
| 3346 | skp = ssp->smk_out; |
Zbigniew Jasinski | 96be7b5 | 2014-12-29 15:34:58 +0100 | [diff] [blame] | 3347 | okp = osp->smk_in; |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3348 | #ifdef CONFIG_AUDIT |
| 3349 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 3350 | smk_ad_setfield_u_net_sk(&ad, other); |
| 3351 | #endif |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3352 | rc = smk_access(skp, okp, MAY_WRITE, &ad); |
| 3353 | rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3354 | if (rc == 0) { |
Zbigniew Jasinski | 96be7b5 | 2014-12-29 15:34:58 +0100 | [diff] [blame] | 3355 | okp = osp->smk_out; |
| 3356 | skp = ssp->smk_in; |
Rafal Krypa | 138a868 | 2015-01-08 18:52:45 +0100 | [diff] [blame] | 3357 | rc = smk_access(okp, skp, MAY_WRITE, &ad); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3358 | rc = smk_bu_note("UDS connect", okp, skp, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3359 | MAY_WRITE, rc); |
| 3360 | } |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3361 | } |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3362 | |
Casey Schaufler | 975d5e5 | 2011-09-26 14:43:39 -0700 | [diff] [blame] | 3363 | /* |
| 3364 | * Cross reference the peer labels for SO_PEERSEC. |
| 3365 | */ |
| 3366 | if (rc == 0) { |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3367 | nsp->smk_packet = ssp->smk_out; |
| 3368 | ssp->smk_packet = osp->smk_out; |
Casey Schaufler | 975d5e5 | 2011-09-26 14:43:39 -0700 | [diff] [blame] | 3369 | } |
| 3370 | |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3371 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | /** |
| 3375 | * smack_unix_may_send - Smack access on UDS |
| 3376 | * @sock: one socket |
| 3377 | * @other: the other socket |
| 3378 | * |
| 3379 | * Return 0 if a subject with the smack of sock could access |
| 3380 | * an object with the smack of other, otherwise an error code |
| 3381 | */ |
| 3382 | static int smack_unix_may_send(struct socket *sock, struct socket *other) |
| 3383 | { |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3384 | struct socket_smack *ssp = sock->sk->sk_security; |
| 3385 | struct socket_smack *osp = other->sk->sk_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3386 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3387 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3388 | |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3389 | #ifdef CONFIG_AUDIT |
| 3390 | struct lsm_network_audit net; |
| 3391 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3392 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3393 | smk_ad_setfield_u_net_sk(&ad, other->sk); |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3394 | #endif |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3395 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3396 | if (smack_privileged(CAP_MAC_OVERRIDE)) |
| 3397 | return 0; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3398 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3399 | rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad); |
| 3400 | rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3401 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | /** |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3405 | * smack_socket_sendmsg - Smack check based on destination host |
| 3406 | * @sock: the socket |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3407 | * @msg: the message |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3408 | * @size: the size of the message |
| 3409 | * |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3410 | * Return 0 if the current subject can write to the destination host. |
| 3411 | * For IPv4 this is only a question if the destination is a single label host. |
| 3412 | * For IPv6 this is a check against the label of the port. |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3413 | */ |
| 3414 | static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
| 3415 | int size) |
| 3416 | { |
| 3417 | struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3418 | #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER) |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 3419 | struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3420 | #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3421 | int rc = 0; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3422 | |
| 3423 | /* |
| 3424 | * Perfectly reasonable for this to be NULL |
| 3425 | */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3426 | if (sip == NULL) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3427 | return 0; |
| 3428 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3429 | switch (sip->sin_family) { |
| 3430 | case AF_INET: |
| 3431 | rc = smack_netlabel_send(sock->sk, sip); |
| 3432 | break; |
| 3433 | case AF_INET6: |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3434 | #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3435 | rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING); |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3436 | #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3437 | break; |
| 3438 | } |
| 3439 | return rc; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3440 | } |
| 3441 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3442 | /** |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3443 | * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3444 | * @sap: netlabel secattr |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3445 | * @ssp: socket security information |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3446 | * |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3447 | * Returns a pointer to a Smack label entry found on the label list. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3448 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3449 | static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap, |
| 3450 | struct socket_smack *ssp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3451 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3452 | struct smack_known *skp; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3453 | int found = 0; |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 3454 | int acat; |
| 3455 | int kcat; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3456 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3457 | if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3458 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3459 | * Looks like a CIPSO packet. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3460 | * If there are flags but no level netlabel isn't |
| 3461 | * behaving the way we expect it to. |
| 3462 | * |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3463 | * Look it up in the label table |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3464 | * Without guidance regarding the smack value |
| 3465 | * for the packet fall back on the network |
| 3466 | * ambient value. |
| 3467 | */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3468 | rcu_read_lock(); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3469 | list_for_each_entry(skp, &smack_known_list, list) { |
| 3470 | if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3471 | continue; |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 3472 | /* |
| 3473 | * Compare the catsets. Use the netlbl APIs. |
| 3474 | */ |
| 3475 | if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) { |
| 3476 | if ((skp->smk_netlabel.flags & |
| 3477 | NETLBL_SECATTR_MLS_CAT) == 0) |
| 3478 | found = 1; |
| 3479 | break; |
| 3480 | } |
| 3481 | for (acat = -1, kcat = -1; acat == kcat; ) { |
Paul Moore | 4fbe63d | 2014-08-01 11:17:37 -0400 | [diff] [blame] | 3482 | acat = netlbl_catmap_walk(sap->attr.mls.cat, |
| 3483 | acat + 1); |
| 3484 | kcat = netlbl_catmap_walk( |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 3485 | skp->smk_netlabel.attr.mls.cat, |
| 3486 | kcat + 1); |
| 3487 | if (acat < 0 || kcat < 0) |
| 3488 | break; |
| 3489 | } |
| 3490 | if (acat == kcat) { |
| 3491 | found = 1; |
| 3492 | break; |
| 3493 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3494 | } |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3495 | rcu_read_unlock(); |
| 3496 | |
| 3497 | if (found) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3498 | return skp; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3499 | |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3500 | if (ssp != NULL && ssp->smk_in == &smack_known_star) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3501 | return &smack_known_web; |
| 3502 | return &smack_known_star; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3503 | } |
| 3504 | if ((sap->flags & NETLBL_SECATTR_SECID) != 0) { |
| 3505 | /* |
| 3506 | * Looks like a fallback, which gives us a secid. |
| 3507 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3508 | skp = smack_from_secid(sap->attr.secid); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3509 | /* |
| 3510 | * This has got to be a bug because it is |
| 3511 | * impossible to specify a fallback without |
| 3512 | * specifying the label, which will ensure |
| 3513 | * it has a secid, and the only way to get a |
| 3514 | * secid is from a fallback. |
| 3515 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3516 | BUG_ON(skp == NULL); |
| 3517 | return skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3518 | } |
| 3519 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3520 | * Without guidance regarding the smack value |
| 3521 | * for the packet fall back on the network |
| 3522 | * ambient value. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3523 | */ |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3524 | return smack_net_ambient; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3525 | } |
| 3526 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3527 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 3528 | static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3529 | { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3530 | u8 nexthdr; |
| 3531 | int offset; |
| 3532 | int proto = -EINVAL; |
| 3533 | struct ipv6hdr _ipv6h; |
| 3534 | struct ipv6hdr *ip6; |
| 3535 | __be16 frag_off; |
| 3536 | struct tcphdr _tcph, *th; |
| 3537 | struct udphdr _udph, *uh; |
| 3538 | struct dccp_hdr _dccph, *dh; |
| 3539 | |
| 3540 | sip->sin6_port = 0; |
| 3541 | |
| 3542 | offset = skb_network_offset(skb); |
| 3543 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 3544 | if (ip6 == NULL) |
| 3545 | return -EINVAL; |
| 3546 | sip->sin6_addr = ip6->saddr; |
| 3547 | |
| 3548 | nexthdr = ip6->nexthdr; |
| 3549 | offset += sizeof(_ipv6h); |
| 3550 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
| 3551 | if (offset < 0) |
| 3552 | return -EINVAL; |
| 3553 | |
| 3554 | proto = nexthdr; |
| 3555 | switch (proto) { |
| 3556 | case IPPROTO_TCP: |
| 3557 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 3558 | if (th != NULL) |
| 3559 | sip->sin6_port = th->source; |
| 3560 | break; |
| 3561 | case IPPROTO_UDP: |
| 3562 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 3563 | if (uh != NULL) |
| 3564 | sip->sin6_port = uh->source; |
| 3565 | break; |
| 3566 | case IPPROTO_DCCP: |
| 3567 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 3568 | if (dh != NULL) |
| 3569 | sip->sin6_port = dh->dccph_sport; |
| 3570 | break; |
| 3571 | } |
| 3572 | return proto; |
| 3573 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3574 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3575 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3576 | /** |
| 3577 | * smack_socket_sock_rcv_skb - Smack packet delivery access check |
| 3578 | * @sk: socket |
| 3579 | * @skb: packet |
| 3580 | * |
| 3581 | * Returns 0 if the packet should be delivered, an error code otherwise |
| 3582 | */ |
| 3583 | static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 3584 | { |
| 3585 | struct netlbl_lsm_secattr secattr; |
| 3586 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3587 | struct smack_known *skp = NULL; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3588 | int rc = 0; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3589 | struct smk_audit_info ad; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3590 | #ifdef CONFIG_AUDIT |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3591 | struct lsm_network_audit net; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3592 | #endif |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3593 | #if IS_ENABLED(CONFIG_IPV6) |
| 3594 | struct sockaddr_in6 sadd; |
| 3595 | int proto; |
| 3596 | #endif /* CONFIG_IPV6 */ |
| 3597 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3598 | switch (sk->sk_family) { |
| 3599 | case PF_INET: |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3600 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3601 | /* |
| 3602 | * If there is a secmark use it rather than the CIPSO label. |
| 3603 | * If there is no secmark fall back to CIPSO. |
| 3604 | * The secmark is assumed to reflect policy better. |
| 3605 | */ |
| 3606 | if (skb && skb->secmark != 0) { |
| 3607 | skp = smack_from_secid(skb->secmark); |
| 3608 | goto access_check; |
| 3609 | } |
| 3610 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3611 | /* |
| 3612 | * Translate what netlabel gave us. |
| 3613 | */ |
| 3614 | netlbl_secattr_init(&secattr); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3615 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3616 | rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr); |
| 3617 | if (rc == 0) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3618 | skp = smack_from_secattr(&secattr, ssp); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3619 | else |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3620 | skp = smack_net_ambient; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3621 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3622 | netlbl_secattr_destroy(&secattr); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3623 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3624 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3625 | access_check: |
| 3626 | #endif |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3627 | #ifdef CONFIG_AUDIT |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3628 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 3629 | ad.a.u.net->family = sk->sk_family; |
| 3630 | ad.a.u.net->netif = skb->skb_iif; |
| 3631 | ipv4_skb_to_auditdata(skb, &ad.a, NULL); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3632 | #endif |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3633 | /* |
| 3634 | * Receiving a packet requires that the other end |
| 3635 | * be able to write here. Read access is not required. |
| 3636 | * This is the simplist possible security model |
| 3637 | * for networking. |
| 3638 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3639 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); |
| 3640 | rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3641 | MAY_WRITE, rc); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3642 | if (rc != 0) |
| 3643 | netlbl_skbuff_err(skb, rc, 0); |
| 3644 | break; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3645 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3646 | case PF_INET6: |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3647 | proto = smk_skb_to_addr_ipv6(skb, &sadd); |
| 3648 | if (proto != IPPROTO_UDP && proto != IPPROTO_TCP) |
| 3649 | break; |
| 3650 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3651 | if (skb && skb->secmark != 0) |
| 3652 | skp = smack_from_secid(skb->secmark); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3653 | else |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3654 | skp = smack_net_ambient; |
| 3655 | #ifdef CONFIG_AUDIT |
| 3656 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 3657 | ad.a.u.net->family = sk->sk_family; |
| 3658 | ad.a.u.net->netif = skb->skb_iif; |
| 3659 | ipv6_skb_to_auditdata(skb, &ad.a, NULL); |
| 3660 | #endif /* CONFIG_AUDIT */ |
| 3661 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); |
| 3662 | rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in, |
| 3663 | MAY_WRITE, rc); |
| 3664 | #else /* CONFIG_SECURITY_SMACK_NETFILTER */ |
| 3665 | rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING); |
| 3666 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3667 | break; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3668 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3669 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3670 | |
Paul Moore | a813429 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 3671 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | /** |
| 3675 | * smack_socket_getpeersec_stream - pull in packet label |
| 3676 | * @sock: the socket |
| 3677 | * @optval: user's destination |
| 3678 | * @optlen: size thereof |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3679 | * @len: max thereof |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3680 | * |
| 3681 | * returns zero on success, an error code otherwise |
| 3682 | */ |
| 3683 | static int smack_socket_getpeersec_stream(struct socket *sock, |
| 3684 | char __user *optval, |
| 3685 | int __user *optlen, unsigned len) |
| 3686 | { |
| 3687 | struct socket_smack *ssp; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3688 | char *rcp = ""; |
| 3689 | int slen = 1; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3690 | int rc = 0; |
| 3691 | |
| 3692 | ssp = sock->sk->sk_security; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3693 | if (ssp->smk_packet != NULL) { |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3694 | rcp = ssp->smk_packet->smk_known; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3695 | slen = strlen(rcp) + 1; |
| 3696 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3697 | |
| 3698 | if (slen > len) |
| 3699 | rc = -ERANGE; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3700 | else if (copy_to_user(optval, rcp, slen) != 0) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3701 | rc = -EFAULT; |
| 3702 | |
| 3703 | if (put_user(slen, optlen) != 0) |
| 3704 | rc = -EFAULT; |
| 3705 | |
| 3706 | return rc; |
| 3707 | } |
| 3708 | |
| 3709 | |
| 3710 | /** |
| 3711 | * smack_socket_getpeersec_dgram - pull in packet label |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3712 | * @sock: the peer socket |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3713 | * @skb: packet data |
| 3714 | * @secid: pointer to where to put the secid of the packet |
| 3715 | * |
| 3716 | * Sets the netlabel socket state on sk from parent |
| 3717 | */ |
| 3718 | static int smack_socket_getpeersec_dgram(struct socket *sock, |
| 3719 | struct sk_buff *skb, u32 *secid) |
| 3720 | |
| 3721 | { |
| 3722 | struct netlbl_lsm_secattr secattr; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3723 | struct socket_smack *ssp = NULL; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3724 | struct smack_known *skp; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3725 | int family = PF_UNSPEC; |
| 3726 | u32 s = 0; /* 0 is the invalid secid */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3727 | int rc; |
| 3728 | |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3729 | if (skb != NULL) { |
| 3730 | if (skb->protocol == htons(ETH_P_IP)) |
| 3731 | family = PF_INET; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3732 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3733 | else if (skb->protocol == htons(ETH_P_IPV6)) |
| 3734 | family = PF_INET6; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3735 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3736 | } |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3737 | if (family == PF_UNSPEC && sock != NULL) |
| 3738 | family = sock->sk->sk_family; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3739 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3740 | switch (family) { |
| 3741 | case PF_UNIX: |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3742 | ssp = sock->sk->sk_security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3743 | s = ssp->smk_out->smk_secid; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3744 | break; |
| 3745 | case PF_INET: |
| 3746 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3747 | s = skb->secmark; |
| 3748 | if (s != 0) |
| 3749 | break; |
| 3750 | #endif |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3751 | /* |
| 3752 | * Translate what netlabel gave us. |
| 3753 | */ |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3754 | if (sock != NULL && sock->sk != NULL) |
| 3755 | ssp = sock->sk->sk_security; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3756 | netlbl_secattr_init(&secattr); |
| 3757 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
| 3758 | if (rc == 0) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3759 | skp = smack_from_secattr(&secattr, ssp); |
| 3760 | s = skp->smk_secid; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3761 | } |
| 3762 | netlbl_secattr_destroy(&secattr); |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3763 | break; |
| 3764 | #if IS_ENABLED(CONFIG_IPV6) |
| 3765 | case PF_INET6: |
| 3766 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3767 | s = skb->secmark; |
| 3768 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ |
| 3769 | break; |
| 3770 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3771 | } |
| 3772 | *secid = s; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3773 | if (s == 0) |
| 3774 | return -EINVAL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3775 | return 0; |
| 3776 | } |
| 3777 | |
| 3778 | /** |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3779 | * smack_sock_graft - Initialize a newly created socket with an existing sock |
| 3780 | * @sk: child sock |
| 3781 | * @parent: parent socket |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3782 | * |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3783 | * Set the smk_{in,out} state of an existing sock based on the process that |
| 3784 | * is creating the new socket. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3785 | */ |
| 3786 | static void smack_sock_graft(struct sock *sk, struct socket *parent) |
| 3787 | { |
| 3788 | struct socket_smack *ssp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3789 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3790 | |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3791 | if (sk == NULL || |
| 3792 | (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3793 | return; |
| 3794 | |
| 3795 | ssp = sk->sk_security; |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3796 | ssp->smk_in = skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3797 | ssp->smk_out = skp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3798 | /* cssp->smk_packet is already set in smack_inet_csk_clone() */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | /** |
| 3802 | * smack_inet_conn_request - Smack access check on connect |
| 3803 | * @sk: socket involved |
| 3804 | * @skb: packet |
| 3805 | * @req: unused |
| 3806 | * |
| 3807 | * Returns 0 if a task with the packet label could write to |
| 3808 | * the socket, otherwise an error code |
| 3809 | */ |
| 3810 | static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 3811 | struct request_sock *req) |
| 3812 | { |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3813 | u16 family = sk->sk_family; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3814 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3815 | struct socket_smack *ssp = sk->sk_security; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3816 | struct netlbl_lsm_secattr secattr; |
| 3817 | struct sockaddr_in addr; |
| 3818 | struct iphdr *hdr; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3819 | struct smack_known *hskp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3820 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3821 | struct smk_audit_info ad; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3822 | #ifdef CONFIG_AUDIT |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3823 | struct lsm_network_audit net; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3824 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3825 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3826 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3827 | if (family == PF_INET6) { |
| 3828 | /* |
| 3829 | * Handle mapped IPv4 packets arriving |
| 3830 | * via IPv6 sockets. Don't set up netlabel |
| 3831 | * processing on IPv6. |
| 3832 | */ |
| 3833 | if (skb->protocol == htons(ETH_P_IP)) |
| 3834 | family = PF_INET; |
| 3835 | else |
| 3836 | return 0; |
| 3837 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3838 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3839 | |
Casey Schaufler | 7f368ad | 2015-02-11 12:52:32 -0800 | [diff] [blame] | 3840 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3841 | /* |
| 3842 | * If there is a secmark use it rather than the CIPSO label. |
| 3843 | * If there is no secmark fall back to CIPSO. |
| 3844 | * The secmark is assumed to reflect policy better. |
| 3845 | */ |
| 3846 | if (skb && skb->secmark != 0) { |
| 3847 | skp = smack_from_secid(skb->secmark); |
| 3848 | goto access_check; |
| 3849 | } |
| 3850 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ |
| 3851 | |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3852 | netlbl_secattr_init(&secattr); |
| 3853 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3854 | if (rc == 0) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3855 | skp = smack_from_secattr(&secattr, ssp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3856 | else |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3857 | skp = &smack_known_huh; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3858 | netlbl_secattr_destroy(&secattr); |
| 3859 | |
Casey Schaufler | 7f368ad | 2015-02-11 12:52:32 -0800 | [diff] [blame] | 3860 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 3861 | access_check: |
| 3862 | #endif |
| 3863 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3864 | #ifdef CONFIG_AUDIT |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3865 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 3866 | ad.a.u.net->family = family; |
| 3867 | ad.a.u.net->netif = skb->skb_iif; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3868 | ipv4_skb_to_auditdata(skb, &ad.a, NULL); |
| 3869 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3870 | /* |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3871 | * Receiving a packet requires that the other end be able to write |
| 3872 | * here. Read access is not required. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3873 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3874 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); |
| 3875 | rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc); |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3876 | if (rc != 0) |
| 3877 | return rc; |
| 3878 | |
| 3879 | /* |
| 3880 | * Save the peer's label in the request_sock so we can later setup |
| 3881 | * smk_packet in the child socket so that SO_PEERCRED can report it. |
| 3882 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3883 | req->peer_secid = skp->smk_secid; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3884 | |
| 3885 | /* |
| 3886 | * We need to decide if we want to label the incoming connection here |
| 3887 | * if we do we only need to label the request_sock and the stack will |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3888 | * propagate the wire-label to the sock when it is created. |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3889 | */ |
| 3890 | hdr = ip_hdr(skb); |
| 3891 | addr.sin_addr.s_addr = hdr->saddr; |
| 3892 | rcu_read_lock(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3893 | hskp = smack_host_label(&addr); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3894 | rcu_read_unlock(); |
| 3895 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3896 | if (hskp == NULL) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3897 | rc = netlbl_req_setattr(req, &skp->smk_netlabel); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3898 | else |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3899 | netlbl_req_delattr(req); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3900 | |
| 3901 | return rc; |
| 3902 | } |
| 3903 | |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3904 | /** |
| 3905 | * smack_inet_csk_clone - Copy the connection information to the new socket |
| 3906 | * @sk: the new socket |
| 3907 | * @req: the connection's request_sock |
| 3908 | * |
| 3909 | * Transfer the connection's peer label to the newly created socket. |
| 3910 | */ |
| 3911 | static void smack_inet_csk_clone(struct sock *sk, |
| 3912 | const struct request_sock *req) |
| 3913 | { |
| 3914 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3915 | struct smack_known *skp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3916 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3917 | if (req->peer_secid != 0) { |
| 3918 | skp = smack_from_secid(req->peer_secid); |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3919 | ssp->smk_packet = skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3920 | } else |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3921 | ssp->smk_packet = NULL; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 3922 | } |
| 3923 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3924 | /* |
| 3925 | * Key management security hooks |
| 3926 | * |
| 3927 | * Casey has not tested key support very heavily. |
| 3928 | * The permission check is most likely too restrictive. |
| 3929 | * If you care about keys please have a look. |
| 3930 | */ |
| 3931 | #ifdef CONFIG_KEYS |
| 3932 | |
| 3933 | /** |
| 3934 | * smack_key_alloc - Set the key security blob |
| 3935 | * @key: object |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3936 | * @cred: the credentials to use |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3937 | * @flags: unused |
| 3938 | * |
| 3939 | * No allocation required |
| 3940 | * |
| 3941 | * Returns 0 |
| 3942 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3943 | static int smack_key_alloc(struct key *key, const struct cred *cred, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3944 | unsigned long flags) |
| 3945 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3946 | struct smack_known *skp = smk_of_task(cred->security); |
| 3947 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3948 | key->security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3949 | return 0; |
| 3950 | } |
| 3951 | |
| 3952 | /** |
| 3953 | * smack_key_free - Clear the key security blob |
| 3954 | * @key: the object |
| 3955 | * |
| 3956 | * Clear the blob pointer |
| 3957 | */ |
| 3958 | static void smack_key_free(struct key *key) |
| 3959 | { |
| 3960 | key->security = NULL; |
| 3961 | } |
| 3962 | |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 3963 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3964 | * smack_key_permission - Smack access on a key |
| 3965 | * @key_ref: gets to the object |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3966 | * @cred: the credentials to use |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 3967 | * @perm: requested key permissions |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3968 | * |
| 3969 | * Return 0 if the task has read and write to the object, |
| 3970 | * an error code otherwise |
| 3971 | */ |
| 3972 | static int smack_key_permission(key_ref_t key_ref, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 3973 | const struct cred *cred, unsigned perm) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3974 | { |
| 3975 | struct key *keyp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3976 | struct smk_audit_info ad; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3977 | struct smack_known *tkp = smk_of_task(cred->security); |
Dmitry Kasatkin | fffea21 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 3978 | int request = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3979 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3980 | |
| 3981 | keyp = key_ref_to_ptr(key_ref); |
| 3982 | if (keyp == NULL) |
| 3983 | return -EINVAL; |
| 3984 | /* |
| 3985 | * If the key hasn't been initialized give it access so that |
| 3986 | * it may do so. |
| 3987 | */ |
| 3988 | if (keyp->security == NULL) |
| 3989 | return 0; |
| 3990 | /* |
| 3991 | * This should not occur |
| 3992 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3993 | if (tkp == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3994 | return -EACCES; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3995 | #ifdef CONFIG_AUDIT |
| 3996 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY); |
| 3997 | ad.a.u.key_struct.key = keyp->serial; |
| 3998 | ad.a.u.key_struct.key_desc = keyp->description; |
| 3999 | #endif |
Dmitry Kasatkin | fffea21 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 4000 | if (perm & KEY_NEED_READ) |
| 4001 | request = MAY_READ; |
| 4002 | if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR)) |
| 4003 | request = MAY_WRITE; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 4004 | rc = smk_access(tkp, keyp->security, request, &ad); |
| 4005 | rc = smk_bu_note("key access", tkp, keyp->security, request, rc); |
| 4006 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4007 | } |
José Bollo | 7fc5f36 | 2015-02-17 15:41:22 +0100 | [diff] [blame] | 4008 | |
| 4009 | /* |
| 4010 | * smack_key_getsecurity - Smack label tagging the key |
| 4011 | * @key points to the key to be queried |
| 4012 | * @_buffer points to a pointer that should be set to point to the |
| 4013 | * resulting string (if no label or an error occurs). |
| 4014 | * Return the length of the string (including terminating NUL) or -ve if |
| 4015 | * an error. |
| 4016 | * May also return 0 (and a NULL buffer pointer) if there is no label. |
| 4017 | */ |
| 4018 | static int smack_key_getsecurity(struct key *key, char **_buffer) |
| 4019 | { |
| 4020 | struct smack_known *skp = key->security; |
| 4021 | size_t length; |
| 4022 | char *copy; |
| 4023 | |
| 4024 | if (key->security == NULL) { |
| 4025 | *_buffer = NULL; |
| 4026 | return 0; |
| 4027 | } |
| 4028 | |
| 4029 | copy = kstrdup(skp->smk_known, GFP_KERNEL); |
| 4030 | if (copy == NULL) |
| 4031 | return -ENOMEM; |
| 4032 | length = strlen(copy) + 1; |
| 4033 | |
| 4034 | *_buffer = copy; |
| 4035 | return length; |
| 4036 | } |
| 4037 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4038 | #endif /* CONFIG_KEYS */ |
| 4039 | |
| 4040 | /* |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4041 | * Smack Audit hooks |
| 4042 | * |
| 4043 | * Audit requires a unique representation of each Smack specific |
| 4044 | * rule. This unique representation is used to distinguish the |
| 4045 | * object to be audited from remaining kernel objects and also |
| 4046 | * works as a glue between the audit hooks. |
| 4047 | * |
| 4048 | * Since repository entries are added but never deleted, we'll use |
| 4049 | * the smack_known label address related to the given audit rule as |
| 4050 | * the needed unique representation. This also better fits the smack |
| 4051 | * model where nearly everything is a label. |
| 4052 | */ |
| 4053 | #ifdef CONFIG_AUDIT |
| 4054 | |
| 4055 | /** |
| 4056 | * smack_audit_rule_init - Initialize a smack audit rule |
| 4057 | * @field: audit rule fields given from user-space (audit.h) |
| 4058 | * @op: required testing operator (=, !=, >, <, ...) |
| 4059 | * @rulestr: smack label to be audited |
| 4060 | * @vrule: pointer to save our own audit rule representation |
| 4061 | * |
| 4062 | * Prepare to audit cases where (@field @op @rulestr) is true. |
| 4063 | * The label to be audited is created if necessay. |
| 4064 | */ |
| 4065 | static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) |
| 4066 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4067 | struct smack_known *skp; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4068 | char **rule = (char **)vrule; |
| 4069 | *rule = NULL; |
| 4070 | |
| 4071 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) |
| 4072 | return -EINVAL; |
| 4073 | |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 4074 | if (op != Audit_equal && op != Audit_not_equal) |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4075 | return -EINVAL; |
| 4076 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4077 | skp = smk_import_entry(rulestr, 0); |
| 4078 | if (skp) |
| 4079 | *rule = skp->smk_known; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4080 | |
| 4081 | return 0; |
| 4082 | } |
| 4083 | |
| 4084 | /** |
| 4085 | * smack_audit_rule_known - Distinguish Smack audit rules |
| 4086 | * @krule: rule of interest, in Audit kernel representation format |
| 4087 | * |
| 4088 | * This is used to filter Smack rules from remaining Audit ones. |
| 4089 | * If it's proved that this rule belongs to us, the |
| 4090 | * audit_rule_match hook will be called to do the final judgement. |
| 4091 | */ |
| 4092 | static int smack_audit_rule_known(struct audit_krule *krule) |
| 4093 | { |
| 4094 | struct audit_field *f; |
| 4095 | int i; |
| 4096 | |
| 4097 | for (i = 0; i < krule->field_count; i++) { |
| 4098 | f = &krule->fields[i]; |
| 4099 | |
| 4100 | if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER) |
| 4101 | return 1; |
| 4102 | } |
| 4103 | |
| 4104 | return 0; |
| 4105 | } |
| 4106 | |
| 4107 | /** |
| 4108 | * smack_audit_rule_match - Audit given object ? |
| 4109 | * @secid: security id for identifying the object to test |
| 4110 | * @field: audit rule flags given from user-space |
| 4111 | * @op: required testing operator |
| 4112 | * @vrule: smack internal rule presentation |
| 4113 | * @actx: audit context associated with the check |
| 4114 | * |
| 4115 | * The core Audit hook. It's used to take the decision of |
| 4116 | * whether to audit or not to audit a given object. |
| 4117 | */ |
| 4118 | static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule, |
| 4119 | struct audit_context *actx) |
| 4120 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4121 | struct smack_known *skp; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4122 | char *rule = vrule; |
| 4123 | |
Richard Guy Briggs | 4eb0f4a | 2013-11-21 13:57:33 -0500 | [diff] [blame] | 4124 | if (unlikely(!rule)) { |
| 4125 | WARN_ONCE(1, "Smack: missing rule\n"); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4126 | return -ENOENT; |
| 4127 | } |
| 4128 | |
| 4129 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) |
| 4130 | return 0; |
| 4131 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4132 | skp = smack_from_secid(secid); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4133 | |
| 4134 | /* |
| 4135 | * No need to do string comparisons. If a match occurs, |
| 4136 | * both pointers will point to the same smack_known |
| 4137 | * label. |
| 4138 | */ |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 4139 | if (op == Audit_equal) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4140 | return (rule == skp->smk_known); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 4141 | if (op == Audit_not_equal) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4142 | return (rule != skp->smk_known); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4143 | |
| 4144 | return 0; |
| 4145 | } |
| 4146 | |
| 4147 | /** |
| 4148 | * smack_audit_rule_free - free smack rule representation |
| 4149 | * @vrule: rule to be freed. |
| 4150 | * |
| 4151 | * No memory was allocated. |
| 4152 | */ |
| 4153 | static void smack_audit_rule_free(void *vrule) |
| 4154 | { |
| 4155 | /* No-op */ |
| 4156 | } |
| 4157 | |
| 4158 | #endif /* CONFIG_AUDIT */ |
| 4159 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 4160 | /** |
David Quigley | 746df9b | 2013-05-22 12:50:35 -0400 | [diff] [blame] | 4161 | * smack_ismaclabel - check if xattr @name references a smack MAC label |
| 4162 | * @name: Full xattr name to check. |
| 4163 | */ |
| 4164 | static int smack_ismaclabel(const char *name) |
| 4165 | { |
| 4166 | return (strcmp(name, XATTR_SMACK_SUFFIX) == 0); |
| 4167 | } |
| 4168 | |
| 4169 | |
| 4170 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4171 | * smack_secid_to_secctx - return the smack label for a secid |
| 4172 | * @secid: incoming integer |
| 4173 | * @secdata: destination |
| 4174 | * @seclen: how long it is |
| 4175 | * |
| 4176 | * Exists for networking code. |
| 4177 | */ |
| 4178 | static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 4179 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4180 | struct smack_known *skp = smack_from_secid(secid); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4181 | |
Eric Paris | d5630b9 | 2010-10-13 16:24:48 -0400 | [diff] [blame] | 4182 | if (secdata) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4183 | *secdata = skp->smk_known; |
| 4184 | *seclen = strlen(skp->smk_known); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4185 | return 0; |
| 4186 | } |
| 4187 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 4188 | /** |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 4189 | * smack_secctx_to_secid - return the secid for a smack label |
| 4190 | * @secdata: smack label |
| 4191 | * @seclen: how long result is |
| 4192 | * @secid: outgoing integer |
| 4193 | * |
| 4194 | * Exists for audit and networking code. |
| 4195 | */ |
David Howells | e52c1764 | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 4196 | static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 4197 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4198 | struct smack_known *skp = smk_find_entry(secdata); |
| 4199 | |
| 4200 | if (skp) |
| 4201 | *secid = skp->smk_secid; |
| 4202 | else |
| 4203 | *secid = 0; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 4204 | return 0; |
| 4205 | } |
| 4206 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 4207 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4208 | * smack_release_secctx - don't do anything. |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 4209 | * @secdata: unused |
| 4210 | * @seclen: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4211 | * |
| 4212 | * Exists to make sure nothing gets done, and properly |
| 4213 | */ |
| 4214 | static void smack_release_secctx(char *secdata, u32 seclen) |
| 4215 | { |
| 4216 | } |
| 4217 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 4218 | static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 4219 | { |
| 4220 | return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0); |
| 4221 | } |
| 4222 | |
| 4223 | static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 4224 | { |
| 4225 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0); |
| 4226 | } |
| 4227 | |
| 4228 | static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 4229 | { |
| 4230 | int len = 0; |
| 4231 | len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true); |
| 4232 | |
| 4233 | if (len < 0) |
| 4234 | return len; |
| 4235 | *ctxlen = len; |
| 4236 | return 0; |
| 4237 | } |
| 4238 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 4239 | struct security_hook_list smack_hooks[] = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4240 | LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check), |
| 4241 | LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme), |
| 4242 | LSM_HOOK_INIT(syslog, smack_syslog), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4243 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4244 | LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), |
| 4245 | LSM_HOOK_INIT(sb_free_security, smack_sb_free_security), |
| 4246 | LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data), |
| 4247 | LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount), |
| 4248 | LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4249 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4250 | LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds), |
| 4251 | LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds), |
| 4252 | LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec), |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4253 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4254 | LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security), |
| 4255 | LSM_HOOK_INIT(inode_free_security, smack_inode_free_security), |
| 4256 | LSM_HOOK_INIT(inode_init_security, smack_inode_init_security), |
| 4257 | LSM_HOOK_INIT(inode_link, smack_inode_link), |
| 4258 | LSM_HOOK_INIT(inode_unlink, smack_inode_unlink), |
| 4259 | LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir), |
| 4260 | LSM_HOOK_INIT(inode_rename, smack_inode_rename), |
| 4261 | LSM_HOOK_INIT(inode_permission, smack_inode_permission), |
| 4262 | LSM_HOOK_INIT(inode_setattr, smack_inode_setattr), |
| 4263 | LSM_HOOK_INIT(inode_getattr, smack_inode_getattr), |
| 4264 | LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr), |
| 4265 | LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr), |
| 4266 | LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr), |
| 4267 | LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr), |
| 4268 | LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity), |
| 4269 | LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity), |
| 4270 | LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity), |
| 4271 | LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4272 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4273 | LSM_HOOK_INIT(file_permission, smack_file_permission), |
| 4274 | LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security), |
| 4275 | LSM_HOOK_INIT(file_free_security, smack_file_free_security), |
| 4276 | LSM_HOOK_INIT(file_ioctl, smack_file_ioctl), |
| 4277 | LSM_HOOK_INIT(file_lock, smack_file_lock), |
| 4278 | LSM_HOOK_INIT(file_fcntl, smack_file_fcntl), |
| 4279 | LSM_HOOK_INIT(mmap_file, smack_mmap_file), |
| 4280 | LSM_HOOK_INIT(mmap_addr, cap_mmap_addr), |
| 4281 | LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner), |
| 4282 | LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask), |
| 4283 | LSM_HOOK_INIT(file_receive, smack_file_receive), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4284 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4285 | LSM_HOOK_INIT(file_open, smack_file_open), |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 4286 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4287 | LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank), |
| 4288 | LSM_HOOK_INIT(cred_free, smack_cred_free), |
| 4289 | LSM_HOOK_INIT(cred_prepare, smack_cred_prepare), |
| 4290 | LSM_HOOK_INIT(cred_transfer, smack_cred_transfer), |
| 4291 | LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as), |
| 4292 | LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as), |
| 4293 | LSM_HOOK_INIT(task_setpgid, smack_task_setpgid), |
| 4294 | LSM_HOOK_INIT(task_getpgid, smack_task_getpgid), |
| 4295 | LSM_HOOK_INIT(task_getsid, smack_task_getsid), |
| 4296 | LSM_HOOK_INIT(task_getsecid, smack_task_getsecid), |
| 4297 | LSM_HOOK_INIT(task_setnice, smack_task_setnice), |
| 4298 | LSM_HOOK_INIT(task_setioprio, smack_task_setioprio), |
| 4299 | LSM_HOOK_INIT(task_getioprio, smack_task_getioprio), |
| 4300 | LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler), |
| 4301 | LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler), |
| 4302 | LSM_HOOK_INIT(task_movememory, smack_task_movememory), |
| 4303 | LSM_HOOK_INIT(task_kill, smack_task_kill), |
| 4304 | LSM_HOOK_INIT(task_wait, smack_task_wait), |
| 4305 | LSM_HOOK_INIT(task_to_inode, smack_task_to_inode), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4306 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4307 | LSM_HOOK_INIT(ipc_permission, smack_ipc_permission), |
| 4308 | LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4309 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4310 | LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security), |
| 4311 | LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4312 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4313 | LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security), |
| 4314 | LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security), |
| 4315 | LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate), |
| 4316 | LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl), |
| 4317 | LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd), |
| 4318 | LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4319 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4320 | LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security), |
| 4321 | LSM_HOOK_INIT(shm_free_security, smack_shm_free_security), |
| 4322 | LSM_HOOK_INIT(shm_associate, smack_shm_associate), |
| 4323 | LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl), |
| 4324 | LSM_HOOK_INIT(shm_shmat, smack_shm_shmat), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4325 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4326 | LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security), |
| 4327 | LSM_HOOK_INIT(sem_free_security, smack_sem_free_security), |
| 4328 | LSM_HOOK_INIT(sem_associate, smack_sem_associate), |
| 4329 | LSM_HOOK_INIT(sem_semctl, smack_sem_semctl), |
| 4330 | LSM_HOOK_INIT(sem_semop, smack_sem_semop), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4331 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4332 | LSM_HOOK_INIT(d_instantiate, smack_d_instantiate), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4333 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4334 | LSM_HOOK_INIT(getprocattr, smack_getprocattr), |
| 4335 | LSM_HOOK_INIT(setprocattr, smack_setprocattr), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4336 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4337 | LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect), |
| 4338 | LSM_HOOK_INIT(unix_may_send, smack_unix_may_send), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4339 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4340 | LSM_HOOK_INIT(socket_post_create, smack_socket_post_create), |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4341 | #ifndef CONFIG_SECURITY_SMACK_NETFILTER |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4342 | LSM_HOOK_INIT(socket_bind, smack_socket_bind), |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4343 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4344 | LSM_HOOK_INIT(socket_connect, smack_socket_connect), |
| 4345 | LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg), |
| 4346 | LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb), |
| 4347 | LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream), |
| 4348 | LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram), |
| 4349 | LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security), |
| 4350 | LSM_HOOK_INIT(sk_free_security, smack_sk_free_security), |
| 4351 | LSM_HOOK_INIT(sock_graft, smack_sock_graft), |
| 4352 | LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request), |
| 4353 | LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone), |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4354 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4355 | /* key management security hooks */ |
| 4356 | #ifdef CONFIG_KEYS |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4357 | LSM_HOOK_INIT(key_alloc, smack_key_alloc), |
| 4358 | LSM_HOOK_INIT(key_free, smack_key_free), |
| 4359 | LSM_HOOK_INIT(key_permission, smack_key_permission), |
| 4360 | LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4361 | #endif /* CONFIG_KEYS */ |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4362 | |
| 4363 | /* Audit hooks */ |
| 4364 | #ifdef CONFIG_AUDIT |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4365 | LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init), |
| 4366 | LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known), |
| 4367 | LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match), |
| 4368 | LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free), |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4369 | #endif /* CONFIG_AUDIT */ |
| 4370 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4371 | LSM_HOOK_INIT(ismaclabel, smack_ismaclabel), |
| 4372 | LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx), |
| 4373 | LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid), |
| 4374 | LSM_HOOK_INIT(release_secctx, smack_release_secctx), |
| 4375 | LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx), |
| 4376 | LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx), |
| 4377 | LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4378 | }; |
| 4379 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 4380 | |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4381 | static __init void init_smack_known_list(void) |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 4382 | { |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4383 | /* |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4384 | * Initialize rule list locks |
| 4385 | */ |
| 4386 | mutex_init(&smack_known_huh.smk_rules_lock); |
| 4387 | mutex_init(&smack_known_hat.smk_rules_lock); |
| 4388 | mutex_init(&smack_known_floor.smk_rules_lock); |
| 4389 | mutex_init(&smack_known_star.smk_rules_lock); |
| 4390 | mutex_init(&smack_known_invalid.smk_rules_lock); |
| 4391 | mutex_init(&smack_known_web.smk_rules_lock); |
| 4392 | /* |
| 4393 | * Initialize rule lists |
| 4394 | */ |
| 4395 | INIT_LIST_HEAD(&smack_known_huh.smk_rules); |
| 4396 | INIT_LIST_HEAD(&smack_known_hat.smk_rules); |
| 4397 | INIT_LIST_HEAD(&smack_known_star.smk_rules); |
| 4398 | INIT_LIST_HEAD(&smack_known_floor.smk_rules); |
| 4399 | INIT_LIST_HEAD(&smack_known_invalid.smk_rules); |
| 4400 | INIT_LIST_HEAD(&smack_known_web.smk_rules); |
| 4401 | /* |
| 4402 | * Create the known labels list |
| 4403 | */ |
Tomasz Stanislawski | 4d7cf4a | 2013-06-11 14:55:13 +0200 | [diff] [blame] | 4404 | smk_insert_entry(&smack_known_huh); |
| 4405 | smk_insert_entry(&smack_known_hat); |
| 4406 | smk_insert_entry(&smack_known_star); |
| 4407 | smk_insert_entry(&smack_known_floor); |
| 4408 | smk_insert_entry(&smack_known_invalid); |
| 4409 | smk_insert_entry(&smack_known_web); |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4412 | /** |
| 4413 | * smack_init - initialize the smack system |
| 4414 | * |
| 4415 | * Returns 0 |
| 4416 | */ |
| 4417 | static __init int smack_init(void) |
| 4418 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4419 | struct cred *cred; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4420 | struct task_smack *tsp; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4421 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 4422 | if (!security_module_enable("smack")) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 4423 | return 0; |
| 4424 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4425 | smack_enabled = 1; |
| 4426 | |
Rohit | 1a5b472 | 2014-10-15 17:40:41 +0530 | [diff] [blame] | 4427 | smack_inode_cache = KMEM_CACHE(inode_smack, 0); |
| 4428 | if (!smack_inode_cache) |
| 4429 | return -ENOMEM; |
| 4430 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4431 | tsp = new_task_smack(&smack_known_floor, &smack_known_floor, |
| 4432 | GFP_KERNEL); |
Rohit | 1a5b472 | 2014-10-15 17:40:41 +0530 | [diff] [blame] | 4433 | if (tsp == NULL) { |
| 4434 | kmem_cache_destroy(smack_inode_cache); |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4435 | return -ENOMEM; |
Rohit | 1a5b472 | 2014-10-15 17:40:41 +0530 | [diff] [blame] | 4436 | } |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4437 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4438 | printk(KERN_INFO "Smack: Initializing.\n"); |
| 4439 | |
| 4440 | /* |
| 4441 | * Set the security state for the initial task. |
| 4442 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4443 | cred = (struct cred *) current->cred; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4444 | cred->security = tsp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4445 | |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4446 | /* initialize the smack_known_list */ |
| 4447 | init_smack_known_list(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4448 | |
| 4449 | /* |
| 4450 | * Register with LSM |
| 4451 | */ |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame^] | 4452 | security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks)); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4453 | |
| 4454 | return 0; |
| 4455 | } |
| 4456 | |
| 4457 | /* |
| 4458 | * Smack requires early initialization in order to label |
| 4459 | * all processes and objects when they are created. |
| 4460 | */ |
| 4461 | security_initcall(smack_init); |