blob: 0ec5e4c22cb2a1066c2b897776ead6d3db72635c [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar3323eec2009-02-04 09:06:58 -05002/*
3 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
5 *
Mimi Zohar3323eec2009-02-04 09:06:58 -05006 * File: integrity_audit.c
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02007 * Audit calls for the integrity subsystem
Mimi Zohar3323eec2009-02-04 09:06:58 -05008 */
9
10#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050012#include <linux/audit.h>
Mimi Zohard726d8d2013-03-18 14:48:02 -040013#include "integrity.h"
Mimi Zohar3323eec2009-02-04 09:06:58 -050014
Mimi Zohard726d8d2013-03-18 14:48:02 -040015static int integrity_audit_info;
Mimi Zohar3323eec2009-02-04 09:06:58 -050016
Mimi Zohar3323eec2009-02-04 09:06:58 -050017/* ima_audit_setup - enable informational auditing messages */
Mimi Zohard726d8d2013-03-18 14:48:02 -040018static int __init integrity_audit_setup(char *str)
Mimi Zohar3323eec2009-02-04 09:06:58 -050019{
20 unsigned long audit;
Mimi Zohar3323eec2009-02-04 09:06:58 -050021
Jingoo Han29707b22014-02-05 15:13:14 +090022 if (!kstrtoul(str, 0, &audit))
Mimi Zohard726d8d2013-03-18 14:48:02 -040023 integrity_audit_info = audit ? 1 : 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -050024 return 1;
25}
Mimi Zohard726d8d2013-03-18 14:48:02 -040026__setup("integrity_audit=", integrity_audit_setup);
Mimi Zohar3323eec2009-02-04 09:06:58 -050027
28void integrity_audit_msg(int audit_msgno, struct inode *inode,
29 const unsigned char *fname, const char *op,
30 const char *cause, int result, int audit_info)
31{
Lakshmi Ramasubramanian2f845882020-06-18 14:10:11 -070032 integrity_audit_message(audit_msgno, inode, fname, op, cause,
33 result, audit_info, 0);
34}
35
36void integrity_audit_message(int audit_msgno, struct inode *inode,
37 const unsigned char *fname, const char *op,
38 const char *cause, int result, int audit_info,
39 int errno)
40{
Mimi Zohar3323eec2009-02-04 09:06:58 -050041 struct audit_buffer *ab;
Tetsuo Handa73a6b44a2013-12-24 20:49:01 +090042 char name[TASK_COMM_LEN];
Mimi Zohar3323eec2009-02-04 09:06:58 -050043
Mimi Zohard726d8d2013-03-18 14:48:02 -040044 if (!integrity_audit_info && audit_info == 1) /* Skip info messages */
Mimi Zohar3323eec2009-02-04 09:06:58 -050045 return;
46
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -040047 ab = audit_log_start(audit_context(), GFP_KERNEL, audit_msgno);
Xiaoke Wang83230352022-01-15 09:11:11 +080048 if (!ab)
49 return;
Eric Paris34c111f2010-04-20 10:21:36 -040050 audit_log_format(ab, "pid=%d uid=%u auid=%u ses=%u",
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -050051 task_pid_nr(current),
Denis Efremove44f1282020-08-24 15:54:35 +030052 from_kuid(&init_user_ns, current_uid()),
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060053 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Mimi Zohar523979a2009-02-11 11:12:28 -050054 audit_get_sessionid(current));
Mimi Zohar3323eec2009-02-04 09:06:58 -050055 audit_log_task_context(ab);
Stefan Berger3d2859d2018-06-04 16:54:53 -040056 audit_log_format(ab, " op=%s cause=%s comm=", op, cause);
Tetsuo Handa73a6b44a2013-12-24 20:49:01 +090057 audit_log_untrustedstring(ab, get_task_comm(name, current));
Mimi Zohar3323eec2009-02-04 09:06:58 -050058 if (fname) {
59 audit_log_format(ab, " name=");
60 audit_log_untrustedstring(ab, fname);
61 }
Kees Cook41fdc302012-01-07 10:41:04 -080062 if (inode) {
63 audit_log_format(ab, " dev=");
64 audit_log_untrustedstring(ab, inode->i_sb->s_id);
65 audit_log_format(ab, " ino=%lu", inode->i_ino);
66 }
Lakshmi Ramasubramanian2f845882020-06-18 14:10:11 -070067 audit_log_format(ab, " res=%d errno=%d", !result, errno);
Mimi Zohar3323eec2009-02-04 09:06:58 -050068 audit_log_end(ab);
69}