Skip to content

Commit e6e3ee6

Browse files
Mimi ZoharJiri Slaby
authored andcommitted
ima: audit log files opened with O_DIRECT flag
commit f9b2a73 upstream. Files are measured or appraised based on the IMA policy. When a file, in policy, is opened with the O_DIRECT flag, a deadlock occurs. The first attempt at resolving this lockdep temporarily removed the O_DIRECT flag and restored it, after calculating the hash. The second attempt introduced the O_DIRECT_HAVELOCK flag. Based on this flag, do_blockdev_direct_IO() would skip taking the i_mutex a second time. The third attempt, by Dmitry Kasatkin, resolves the i_mutex locking issue, by re-introducing the IMA mutex, but uncovered another problem. Reading a file with O_DIRECT flag set, writes directly to userspace pages. A second patch allocates a user-space like memory. This works for all IMA hooks, except ima_file_free(), which is called on __fput() to recalculate the file hash. Until this last issue is addressed, do not 'collect' the measurement for measuring, appraising, or auditing files opened with the O_DIRECT flag set. Based on policy, permit or deny file access. This patch defines a new IMA policy rule option named 'permit_directio'. Policy rules could be defined, based on LSM or other criteria, to permit specific applications to open files with the O_DIRECT flag set. Changelog v1: - permit or deny file access based IMA policy rules Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com> Acked-by: Dmitry Kasatkin <d.kasatkin@samsung.com> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent a2793aa commit e6e3ee6

5 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎Documentation/ABI/testing/ima_policy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Description:
2323
[fowner]]
2424
lsm: [[subj_user=] [subj_role=] [subj_type=]
2525
[obj_user=] [obj_role=] [obj_type=]]
26-
option: [[appraise_type=]]
26+
option: [[appraise_type=]] [permit_directio]
2727

2828
base: func:= [BPRM_CHECK][MMAP_CHECK][FILE_CHECK][MODULE_CHECK]
2929
mask:= [MAY_READ] [MAY_WRITE] [MAY_APPEND] [MAY_EXEC]

‎security/integrity/ima/ima_api.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,30 @@ int ima_must_measure(struct inode *inode, int mask, int function)
140140
int ima_collect_measurement(struct integrity_iint_cache *iint,
141141
struct file *file)
142142
{
143+
const char *audit_cause = "failed";
143144
struct inode *inode = file_inode(file);
144145
const char *filename = file->f_dentry->d_name.name;
145146
int result = 0;
146147

147148
if (!(iint->flags & IMA_COLLECTED)) {
148149
u64 i_version = file_inode(file)->i_version;
149150

151+
if (file->f_flags & O_DIRECT) {
152+
audit_cause = "failed(directio)";
153+
result = -EACCES;
154+
goto out;
155+
}
150156
iint->ima_xattr.type = IMA_XATTR_DIGEST;
151157
result = ima_calc_file_hash(file, iint->ima_xattr.digest);
152158
if (!result) {
153159
iint->version = i_version;
154160
iint->flags |= IMA_COLLECTED;
155161
}
156162
}
163+
out:
157164
if (result)
158165
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
159-
filename, "collect_data", "failed",
166+
filename, "collect_data", audit_cause,
160167
result, 0);
161168
return result;
162169
}

‎security/integrity/ima/ima_main.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ static int process_measurement(struct file *file, const char *filename,
186186
}
187187

188188
rc = ima_collect_measurement(iint, file);
189-
if (rc != 0)
189+
if (rc != 0) {
190+
if (file->f_flags & O_DIRECT)
191+
rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
190192
goto out_digsig;
193+
}
191194

192195
pathname = !filename ? ima_d_path(&file->f_path, &pathbuf) : filename;
193196
if (!pathname)

‎security/integrity/ima/ima_policy.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ enum {
351351
Opt_obj_user, Opt_obj_role, Opt_obj_type,
352352
Opt_subj_user, Opt_subj_role, Opt_subj_type,
353353
Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner,
354-
Opt_appraise_type, Opt_fsuuid
354+
Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
355355
};
356356

357357
static match_table_t policy_tokens = {
@@ -373,6 +373,7 @@ static match_table_t policy_tokens = {
373373
{Opt_uid, "uid=%s"},
374374
{Opt_fowner, "fowner=%s"},
375375
{Opt_appraise_type, "appraise_type=%s"},
376+
{Opt_permit_directio, "permit_directio"},
376377
{Opt_err, NULL}
377378
};
378379

@@ -621,6 +622,9 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
621622
else
622623
result = -EINVAL;
623624
break;
625+
case Opt_permit_directio:
626+
entry->flags |= IMA_PERMIT_DIRECTIO;
627+
break;
624628
case Opt_err:
625629
ima_log_string(ab, "UNKNOWN", p);
626630
result = -EINVAL;

‎security/integrity/integrity.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define IMA_ACTION_FLAGS 0xff000000
3131
#define IMA_DIGSIG 0x01000000
3232
#define IMA_DIGSIG_REQUIRED 0x02000000
33+
#define IMA_PERMIT_DIRECTIO 0x04000000
3334

3435
#define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \
3536
IMA_APPRAISE_SUBMASK)

0 commit comments

Comments
 (0)