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/init.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, 293 xattr_len, 294 iint->ima_hash->digest, 295 iint->ima_hash->length); 296 if (rc == -EOPNOTSUPP) { 297 status = INTEGRITY_UNKNOWN; 298 break; 299 } 300 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 301 func == KEXEC_KERNEL_CHECK) 302 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 303 (const char *)xattr_value, 304 xattr_len, 305 iint->ima_hash->digest, 306 iint->ima_hash->length); 307 if (rc) { 308 cause = "invalid-signature"; 309 status = INTEGRITY_FAIL; 310 } else { 311 status = INTEGRITY_PASS; 312 } 313 break; 314 default: 315 status = INTEGRITY_UNKNOWN; 316 cause = "unknown-ima-data"; 317 break; 318 } 319 320 out: 321 /* 322 * File signatures on some filesystems can not be properly verified. 323 * When such filesystems are mounted by an untrusted mounter or on a 324 * system not willing to accept such a risk, fail the file signature 325 * verification. 326 */ 327 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 328 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 329 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 330 status = INTEGRITY_FAIL; 331 cause = "unverifiable-signature"; 332 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 333 op, cause, rc, 0); 334 } else if (status != INTEGRITY_PASS) { 335 /* Fix mode, but don't replace file signatures. */ 336 if ((ima_appraise & IMA_APPRAISE_FIX) && 337 (!xattr_value || 338 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 339 if (!ima_fix_xattr(dentry, iint)) 340 status = INTEGRITY_PASS; 341 } 342 343 /* Permit new files with file signatures, but without data. */ 344 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 345 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) { 346 status = INTEGRITY_PASS; 347 } 348 349 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 350 op, cause, rc, 0); 351 } else { 352 ima_cache_flags(iint, func); 353 } 354 355 ima_set_cache_status(iint, func, status); 356 return status; 357 } 358 359 /* 360 * ima_update_xattr - update 'security.ima' hash value 361 */ 362 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 363 { 364 struct dentry *dentry = file_dentry(file); 365 int rc = 0; 366 367 /* do not collect and update hash for digital signatures */ 368 if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 369 return; 370 371 if ((iint->ima_file_status != INTEGRITY_PASS) && 372 !(iint->flags & IMA_HASH)) 373 return; 374 375 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo); 376 if (rc < 0) 377 return; 378 379 inode_lock(file_inode(file)); 380 ima_fix_xattr(dentry, iint); 381 inode_unlock(file_inode(file)); 382 } 383 384 /** 385 * ima_inode_post_setattr - reflect file metadata changes 386 * @dentry: pointer to the affected dentry 387 * 388 * Changes to a dentry's metadata might result in needing to appraise. 389 * 390 * This function is called from notify_change(), which expects the caller 391 * to lock the inode's i_mutex. 392 */ 393 void ima_inode_post_setattr(struct dentry *dentry) 394 { 395 struct inode *inode = d_backing_inode(dentry); 396 struct integrity_iint_cache *iint; 397 int action; 398 399 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 400 || !(inode->i_opflags & IOP_XATTR)) 401 return; 402 403 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); 404 if (!action) 405 __vfs_removexattr(dentry, XATTR_NAME_IMA); 406 iint = integrity_iint_find(inode); 407 if (iint) { 408 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 409 if (!action) 410 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 411 } 412 } 413 414 /* 415 * ima_protect_xattr - protect 'security.ima' 416 * 417 * Ensure that not just anyone can modify or remove 'security.ima'. 418 */ 419 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 420 const void *xattr_value, size_t xattr_value_len) 421 { 422 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 423 if (!capable(CAP_SYS_ADMIN)) 424 return -EPERM; 425 return 1; 426 } 427 return 0; 428 } 429 430 static void ima_reset_appraise_flags(struct inode *inode, int digsig) 431 { 432 struct integrity_iint_cache *iint; 433 434 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 435 return; 436 437 iint = integrity_iint_find(inode); 438 if (!iint) 439 return; 440 iint->measured_pcrs = 0; 441 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 442 if (digsig) 443 set_bit(IMA_DIGSIG, &iint->atomic_flags); 444 else 445 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 446 } 447 448 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 449 const void *xattr_value, size_t xattr_value_len) 450 { 451 const struct evm_ima_xattr_data *xvalue = xattr_value; 452 int result; 453 454 result = ima_protect_xattr(dentry, xattr_name, xattr_value, 455 xattr_value_len); 456 if (result == 1) { 457 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 458 return -EINVAL; 459 ima_reset_appraise_flags(d_backing_inode(dentry), 460 xvalue->type == EVM_IMA_XATTR_DIGSIG); 461 result = 0; 462 } 463 return result; 464 } 465 466 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 467 { 468 int result; 469 470 result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 471 if (result == 1) { 472 ima_reset_appraise_flags(d_backing_inode(dentry), 0); 473 result = 0; 474 } 475 return result; 476 } 477