1 /* 2 * Copyright (C) 2011 IBM Corporation 3 * 4 * Author: 5 * Mimi Zohar <zohar@us.ibm.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, version 2 of the License. 10 */ 11 #include <linux/module.h> 12 #include <linux/file.h> 13 #include <linux/fs.h> 14 #include <linux/xattr.h> 15 #include <linux/magic.h> 16 #include <linux/ima.h> 17 #include <linux/evm.h> 18 19 #include "ima.h" 20 21 static int __init default_appraise_setup(char *str) 22 { 23 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM 24 if (strncmp(str, "off", 3) == 0) 25 ima_appraise = 0; 26 else if (strncmp(str, "log", 3) == 0) 27 ima_appraise = IMA_APPRAISE_LOG; 28 else if (strncmp(str, "fix", 3) == 0) 29 ima_appraise = IMA_APPRAISE_FIX; 30 #endif 31 return 1; 32 } 33 34 __setup("ima_appraise=", default_appraise_setup); 35 36 /* 37 * is_ima_appraise_enabled - return appraise status 38 * 39 * Only return enabled, if not in ima_appraise="fix" or "log" modes. 40 */ 41 bool is_ima_appraise_enabled(void) 42 { 43 return ima_appraise & IMA_APPRAISE_ENFORCE; 44 } 45 46 /* 47 * ima_must_appraise - set appraise flag 48 * 49 * Return 1 to appraise or hash 50 */ 51 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func) 52 { 53 u32 secid; 54 55 if (!ima_appraise) 56 return 0; 57 58 security_task_getsecid(current, &secid); 59 return ima_match_policy(inode, current_cred(), secid, func, mask, 60 IMA_APPRAISE | IMA_HASH, NULL); 61 } 62 63 static int ima_fix_xattr(struct dentry *dentry, 64 struct integrity_iint_cache *iint) 65 { 66 int rc, offset; 67 u8 algo = iint->ima_hash->algo; 68 69 if (algo <= HASH_ALGO_SHA1) { 70 offset = 1; 71 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 72 } else { 73 offset = 0; 74 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 75 iint->ima_hash->xattr.ng.algo = algo; 76 } 77 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, 78 &iint->ima_hash->xattr.data[offset], 79 (sizeof(iint->ima_hash->xattr) - offset) + 80 iint->ima_hash->length, 0); 81 return rc; 82 } 83 84 /* Return specific func appraised cached result */ 85 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 86 enum ima_hooks func) 87 { 88 switch (func) { 89 case MMAP_CHECK: 90 return iint->ima_mmap_status; 91 case BPRM_CHECK: 92 return iint->ima_bprm_status; 93 case CREDS_CHECK: 94 return iint->ima_creds_status; 95 case FILE_CHECK: 96 case POST_SETATTR: 97 return iint->ima_file_status; 98 case MODULE_CHECK ... MAX_CHECK - 1: 99 default: 100 return iint->ima_read_status; 101 } 102 } 103 104 static void ima_set_cache_status(struct integrity_iint_cache *iint, 105 enum ima_hooks func, 106 enum integrity_status status) 107 { 108 switch (func) { 109 case MMAP_CHECK: 110 iint->ima_mmap_status = status; 111 break; 112 case BPRM_CHECK: 113 iint->ima_bprm_status = status; 114 break; 115 case CREDS_CHECK: 116 iint->ima_creds_status = status; 117 case FILE_CHECK: 118 case POST_SETATTR: 119 iint->ima_file_status = status; 120 break; 121 case MODULE_CHECK ... MAX_CHECK - 1: 122 default: 123 iint->ima_read_status = status; 124 break; 125 } 126 } 127 128 static void ima_cache_flags(struct integrity_iint_cache *iint, 129 enum ima_hooks func) 130 { 131 switch (func) { 132 case MMAP_CHECK: 133 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 134 break; 135 case BPRM_CHECK: 136 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 137 break; 138 case CREDS_CHECK: 139 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 140 break; 141 case FILE_CHECK: 142 case POST_SETATTR: 143 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 144 break; 145 case MODULE_CHECK ... MAX_CHECK - 1: 146 default: 147 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 148 break; 149 } 150 } 151 152 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, 153 int xattr_len) 154 { 155 struct signature_v2_hdr *sig; 156 enum hash_algo ret; 157 158 if (!xattr_value || xattr_len < 2) 159 /* return default hash algo */ 160 return ima_hash_algo; 161 162 switch (xattr_value->type) { 163 case EVM_IMA_XATTR_DIGSIG: 164 sig = (typeof(sig))xattr_value; 165 if (sig->version != 2 || xattr_len <= sizeof(*sig)) 166 return ima_hash_algo; 167 return sig->hash_algo; 168 break; 169 case IMA_XATTR_DIGEST_NG: 170 ret = xattr_value->digest[0]; 171 if (ret < HASH_ALGO__LAST) 172 return ret; 173 break; 174 case IMA_XATTR_DIGEST: 175 /* this is for backward compatibility */ 176 if (xattr_len == 21) { 177 unsigned int zero = 0; 178 if (!memcmp(&xattr_value->digest[16], &zero, 4)) 179 return HASH_ALGO_MD5; 180 else 181 return HASH_ALGO_SHA1; 182 } else if (xattr_len == 17) 183 return HASH_ALGO_MD5; 184 break; 185 } 186 187 /* return default hash algo */ 188 return ima_hash_algo; 189 } 190 191 int ima_read_xattr(struct dentry *dentry, 192 struct evm_ima_xattr_data **xattr_value) 193 { 194 ssize_t ret; 195 196 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value, 197 0, GFP_NOFS); 198 if (ret == -EOPNOTSUPP) 199 ret = 0; 200 return ret; 201 } 202 203 /* 204 * ima_appraise_measurement - appraise file measurement 205 * 206 * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 207 * Assuming success, compare the xattr hash with the collected measurement. 208 * 209 * Return 0 on success, error code otherwise 210 */ 211 int ima_appraise_measurement(enum ima_hooks func, 212 struct integrity_iint_cache *iint, 213 struct file *file, const unsigned char *filename, 214 struct evm_ima_xattr_data *xattr_value, 215 int xattr_len) 216 { 217 static const char op[] = "appraise_data"; 218 const char *cause = "unknown"; 219 struct dentry *dentry = file_dentry(file); 220 struct inode *inode = d_backing_inode(dentry); 221 enum integrity_status status = INTEGRITY_UNKNOWN; 222 int rc = xattr_len, hash_start = 0; 223 224 if (!(inode->i_opflags & IOP_XATTR)) 225 return INTEGRITY_UNKNOWN; 226 227 if (rc <= 0) { 228 if (rc && rc != -ENODATA) 229 goto out; 230 231 cause = iint->flags & IMA_DIGSIG_REQUIRED ? 232 "IMA-signature-required" : "missing-hash"; 233 status = INTEGRITY_NOLABEL; 234 if (file->f_mode & FMODE_CREATED) 235 iint->flags |= IMA_NEW_FILE; 236 if ((iint->flags & IMA_NEW_FILE) && 237 (!(iint->flags & IMA_DIGSIG_REQUIRED) || 238 (inode->i_size == 0))) 239 status = INTEGRITY_PASS; 240 goto out; 241 } 242 243 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); 244 switch (status) { 245 case INTEGRITY_PASS: 246 case INTEGRITY_PASS_IMMUTABLE: 247 case INTEGRITY_UNKNOWN: 248 break; 249 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 250 case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 251 cause = "missing-HMAC"; 252 goto out; 253 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 254 cause = "invalid-HMAC"; 255 goto out; 256 default: 257 WARN_ONCE(true, "Unexpected integrity status %d\n", status); 258 } 259 260 switch (xattr_value->type) { 261 case IMA_XATTR_DIGEST_NG: 262 /* first byte contains algorithm id */ 263 hash_start = 1; 264 /* fall through */ 265 case IMA_XATTR_DIGEST: 266 if (iint->flags & IMA_DIGSIG_REQUIRED) { 267 cause = "IMA-signature-required"; 268 status = INTEGRITY_FAIL; 269 break; 270 } 271 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 272 if (xattr_len - sizeof(xattr_value->type) - hash_start >= 273 iint->ima_hash->length) 274 /* xattr length may be longer. md5 hash in previous 275 version occupied 20 bytes in xattr, instead of 16 276 */ 277 rc = memcmp(&xattr_value->digest[hash_start], 278 iint->ima_hash->digest, 279 iint->ima_hash->length); 280 else 281 rc = -EINVAL; 282 if (rc) { 283 cause = "invalid-hash"; 284 status = INTEGRITY_FAIL; 285 break; 286 } 287 status = INTEGRITY_PASS; 288 break; 289 case EVM_IMA_XATTR_DIGSIG: 290 set_bit(IMA_DIGSIG, &iint->atomic_flags); 291 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 292 (const char *)xattr_value, rc, 293 iint->ima_hash->digest, 294 iint->ima_hash->length); 295 if (rc == -EOPNOTSUPP) { 296 status = INTEGRITY_UNKNOWN; 297 } else if (rc) { 298 cause = "invalid-signature"; 299 status = INTEGRITY_FAIL; 300 } else { 301 status = INTEGRITY_PASS; 302 } 303 break; 304 default: 305 status = INTEGRITY_UNKNOWN; 306 cause = "unknown-ima-data"; 307 break; 308 } 309 310 out: 311 /* 312 * File signatures on some filesystems can not be properly verified. 313 * When such filesystems are mounted by an untrusted mounter or on a 314 * system not willing to accept such a risk, fail the file signature 315 * verification. 316 */ 317 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 318 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 319 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 320 status = INTEGRITY_FAIL; 321 cause = "unverifiable-signature"; 322 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 323 op, cause, rc, 0); 324 } else if (status != INTEGRITY_PASS) { 325 /* Fix mode, but don't replace file signatures. */ 326 if ((ima_appraise & IMA_APPRAISE_FIX) && 327 (!xattr_value || 328 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 329 if (!ima_fix_xattr(dentry, iint)) 330 status = INTEGRITY_PASS; 331 } 332 333 /* Permit new files with file signatures, but without data. */ 334 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 335 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) { 336 status = INTEGRITY_PASS; 337 } 338 339 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 340 op, cause, rc, 0); 341 } else { 342 ima_cache_flags(iint, func); 343 } 344 345 ima_set_cache_status(iint, func, status); 346 return status; 347 } 348 349 /* 350 * ima_update_xattr - update 'security.ima' hash value 351 */ 352 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 353 { 354 struct dentry *dentry = file_dentry(file); 355 int rc = 0; 356 357 /* do not collect and update hash for digital signatures */ 358 if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 359 return; 360 361 if ((iint->ima_file_status != INTEGRITY_PASS) && 362 !(iint->flags & IMA_HASH)) 363 return; 364 365 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo); 366 if (rc < 0) 367 return; 368 369 inode_lock(file_inode(file)); 370 ima_fix_xattr(dentry, iint); 371 inode_unlock(file_inode(file)); 372 } 373 374 /** 375 * ima_inode_post_setattr - reflect file metadata changes 376 * @dentry: pointer to the affected dentry 377 * 378 * Changes to a dentry's metadata might result in needing to appraise. 379 * 380 * This function is called from notify_change(), which expects the caller 381 * to lock the inode's i_mutex. 382 */ 383 void ima_inode_post_setattr(struct dentry *dentry) 384 { 385 struct inode *inode = d_backing_inode(dentry); 386 struct integrity_iint_cache *iint; 387 int action; 388 389 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 390 || !(inode->i_opflags & IOP_XATTR)) 391 return; 392 393 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); 394 if (!action) 395 __vfs_removexattr(dentry, XATTR_NAME_IMA); 396 iint = integrity_iint_find(inode); 397 if (iint) { 398 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 399 if (!action) 400 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 401 } 402 } 403 404 /* 405 * ima_protect_xattr - protect 'security.ima' 406 * 407 * Ensure that not just anyone can modify or remove 'security.ima'. 408 */ 409 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 410 const void *xattr_value, size_t xattr_value_len) 411 { 412 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 413 if (!capable(CAP_SYS_ADMIN)) 414 return -EPERM; 415 return 1; 416 } 417 return 0; 418 } 419 420 static void ima_reset_appraise_flags(struct inode *inode, int digsig) 421 { 422 struct integrity_iint_cache *iint; 423 424 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 425 return; 426 427 iint = integrity_iint_find(inode); 428 if (!iint) 429 return; 430 iint->measured_pcrs = 0; 431 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 432 if (digsig) 433 set_bit(IMA_DIGSIG, &iint->atomic_flags); 434 else 435 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 436 } 437 438 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 439 const void *xattr_value, size_t xattr_value_len) 440 { 441 const struct evm_ima_xattr_data *xvalue = xattr_value; 442 int result; 443 444 result = ima_protect_xattr(dentry, xattr_name, xattr_value, 445 xattr_value_len); 446 if (result == 1) { 447 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 448 return -EINVAL; 449 ima_reset_appraise_flags(d_backing_inode(dentry), 450 xvalue->type == EVM_IMA_XATTR_DIGSIG); 451 result = 0; 452 } 453 return result; 454 } 455 456 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 457 { 458 int result; 459 460 result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 461 if (result == 1) { 462 ima_reset_appraise_flags(d_backing_inode(dentry), 0); 463 result = 0; 464 } 465 return result; 466 } 467