Merge branch 'next-lockdown' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[sfrench/cifs-2.6.git] / security / integrity / ima / ima_main.c
index 1747bc7bcb604b8e9b3beb8f39b1355d8d9f52d8..60027c643ecdf79ba00ea664153f103b2960e54e 100644 (file)
@@ -39,6 +39,10 @@ int ima_appraise;
 int ima_hash_algo = HASH_ALGO_SHA1;
 static int hash_setup_done;
 
+static struct notifier_block ima_lsm_policy_notifier = {
+       .notifier_call = ima_lsm_policy_change,
+};
+
 static int __init hash_setup(char *str)
 {
        struct ima_template_desc *template_desc = ima_template_desc_current();
@@ -68,6 +72,27 @@ out:
 }
 __setup("ima_hash=", hash_setup);
 
+/* Prevent mmap'ing a file execute that is already mmap'ed write */
+static int mmap_violation_check(enum ima_hooks func, struct file *file,
+                               char **pathbuf, const char **pathname,
+                               char *filename)
+{
+       struct inode *inode;
+       int rc = 0;
+
+       if ((func == MMAP_CHECK) && mapping_writably_mapped(file->f_mapping)) {
+               rc = -ETXTBSY;
+               inode = file_inode(file);
+
+               if (!*pathbuf)  /* ima_rdwr_violation possibly pre-fetched */
+                       *pathname = ima_d_path(&file->f_path, pathbuf,
+                                              filename);
+               integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
+                                   "mmap_file", "mmapped_writers", rc, 0);
+       }
+       return rc;
+}
+
 /*
  * ima_rdwr_violation_check
  *
@@ -170,13 +195,14 @@ static int process_measurement(struct file *file, const struct cred *cred,
 {
        struct inode *inode = file_inode(file);
        struct integrity_iint_cache *iint = NULL;
-       struct ima_template_desc *template_desc;
+       struct ima_template_desc *template_desc = NULL;
        char *pathbuf = NULL;
        char filename[NAME_MAX];
        const char *pathname = NULL;
        int rc = 0, action, must_appraise = 0;
        int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
        struct evm_ima_xattr_data *xattr_value = NULL;
+       struct modsig *modsig = NULL;
        int xattr_len = 0;
        bool violation_check;
        enum hash_algo hash_algo;
@@ -188,7 +214,8 @@ static int process_measurement(struct file *file, const struct cred *cred,
         * bitmask based on the appraise/audit/measurement policy.
         * Included is the appraise submask.
         */
-       action = ima_get_action(inode, cred, secid, mask, func, &pcr);
+       action = ima_get_action(inode, cred, secid, mask, func, &pcr,
+                               &template_desc);
        violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
                           (ima_policy_flag & IMA_MEASURE));
        if (!action && !violation_check)
@@ -266,20 +293,37 @@ static int process_measurement(struct file *file, const struct cred *cred,
 
        /* Nothing to do, just return existing appraised status */
        if (!action) {
-               if (must_appraise)
-                       rc = ima_get_cache_status(iint, func);
+               if (must_appraise) {
+                       rc = mmap_violation_check(func, file, &pathbuf,
+                                                 &pathname, filename);
+                       if (!rc)
+                               rc = ima_get_cache_status(iint, func);
+               }
                goto out_locked;
        }
 
-       template_desc = ima_template_desc_current();
        if ((action & IMA_APPRAISE_SUBMASK) ||
-                   strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
+           strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) {
                /* read 'security.ima' */
                xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
 
+               /*
+                * Read the appended modsig if allowed by the policy, and allow
+                * an additional measurement list entry, if needed, based on the
+                * template format and whether the file was already measured.
+                */
+               if (iint->flags & IMA_MODSIG_ALLOWED) {
+                       rc = ima_read_modsig(func, buf, size, &modsig);
+
+                       if (!rc && ima_template_has_modsig(template_desc) &&
+                           iint->flags & IMA_MEASURED)
+                               action |= IMA_MEASURE;
+               }
+       }
+
        hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
 
-       rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
+       rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig);
        if (rc != 0 && rc != -EBADF && rc != -EINVAL)
                goto out_locked;
 
@@ -288,12 +332,16 @@ static int process_measurement(struct file *file, const struct cred *cred,
 
        if (action & IMA_MEASURE)
                ima_store_measurement(iint, file, pathname,
-                                     xattr_value, xattr_len, pcr);
+                                     xattr_value, xattr_len, modsig, pcr,
+                                     template_desc);
        if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
                inode_lock(inode);
                rc = ima_appraise_measurement(func, iint, file, pathname,
-                                             xattr_value, xattr_len);
+                                             xattr_value, xattr_len, modsig);
                inode_unlock(inode);
+               if (!rc)
+                       rc = mmap_violation_check(func, file, &pathbuf,
+                                                 &pathname, filename);
        }
        if (action & IMA_AUDIT)
                ima_audit_measurement(iint, pathname);
@@ -306,6 +354,7 @@ out_locked:
                rc = -EACCES;
        mutex_unlock(&iint->mutex);
        kfree(xattr_value);
+       ima_free_modsig(modsig);
 out:
        if (pathbuf)
                __putname(pathbuf);
@@ -572,6 +621,80 @@ int ima_load_data(enum kernel_load_data_id id)
        return 0;
 }
 
+/*
+ * process_buffer_measurement - Measure the buffer to ima log.
+ * @buf: pointer to the buffer that needs to be added to the log.
+ * @size: size of buffer(in bytes).
+ * @eventname: event name to be used for the buffer entry.
+ * @cred: a pointer to a credentials structure for user validation.
+ * @secid: the secid of the task to be validated.
+ *
+ * Based on policy, the buffer is measured into the ima log.
+ */
+static void process_buffer_measurement(const void *buf, int size,
+                                      const char *eventname,
+                                      const struct cred *cred, u32 secid)
+{
+       int ret = 0;
+       struct ima_template_entry *entry = NULL;
+       struct integrity_iint_cache iint = {};
+       struct ima_event_data event_data = {.iint = &iint,
+                                           .filename = eventname,
+                                           .buf = buf,
+                                           .buf_len = size};
+       struct ima_template_desc *template_desc = NULL;
+       struct {
+               struct ima_digest_data hdr;
+               char digest[IMA_MAX_DIGEST_SIZE];
+       } hash = {};
+       int violation = 0;
+       int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
+       int action = 0;
+
+       action = ima_get_action(NULL, cred, secid, 0, KEXEC_CMDLINE, &pcr,
+                               &template_desc);
+       if (!(action & IMA_MEASURE))
+               return;
+
+       iint.ima_hash = &hash.hdr;
+       iint.ima_hash->algo = ima_hash_algo;
+       iint.ima_hash->length = hash_digest_size[ima_hash_algo];
+
+       ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
+       if (ret < 0)
+               goto out;
+
+       ret = ima_alloc_init_template(&event_data, &entry, template_desc);
+       if (ret < 0)
+               goto out;
+
+       ret = ima_store_template(entry, violation, NULL, buf, pcr);
+
+       if (ret < 0)
+               ima_free_template_entry(entry);
+
+out:
+       return;
+}
+
+/**
+ * ima_kexec_cmdline - measure kexec cmdline boot args
+ * @buf: pointer to buffer
+ * @size: size of buffer
+ *
+ * Buffers can only be measured, not appraised.
+ */
+void ima_kexec_cmdline(const void *buf, int size)
+{
+       u32 secid;
+
+       if (buf && size != 0) {
+               security_task_getsecid(current, &secid);
+               process_buffer_measurement(buf, size, "kexec-cmdline",
+                                          current_cred(), secid);
+       }
+}
+
 static int __init init_ima(void)
 {
        int error;
@@ -589,6 +712,10 @@ static int __init init_ima(void)
                error = ima_init();
        }
 
+       error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
+       if (error)
+               pr_warn("Couldn't register LSM notifier, error %d\n", error);
+
        if (!error)
                ima_update_policy_flag();