xref: /xnu-8019.80.24/bsd/vfs/vfs_fslog.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2006-2017 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <sys/errno.h>
30 #include <sys/types.h>
31 #include <kern/kalloc.h>
32 #include <sys/buf.h>
33 #include <sys/time.h>
34 #include <sys/kauth.h>
35 #include <sys/mount.h>
36 #include <sys/vnode.h>
37 #include <sys/syslog.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/fslog.h>
40 #include <sys/mount_internal.h>
41 #include <sys/kasl.h>
42 
43 #include <kern/zalloc.h>
44 
45 #include <uuid/uuid.h>
46 
47 #include <stdarg.h>
48 
49 /* Log information about external modification of a process,
50  * using MessageTracer formatting. Assumes that both the caller
51  * and target are appropriately locked.
52  * Currently prints following information -
53  *      1. Caller process name (truncated to 16 characters)
54  *	2. Caller process Mach-O UUID
55  *  3. Target process name (truncated to 16 characters)
56  *  4. Target process Mach-O UUID
57  */
58 void
fslog_extmod_msgtracer(proc_t caller,proc_t target)59 fslog_extmod_msgtracer(proc_t caller, proc_t target)
60 {
61 	if ((caller != PROC_NULL) && (target != PROC_NULL)) {
62 		/*
63 		 * Print into buffer large enough for "ThisIsAnApplicat(BC223DD7-B314-42E0-B6B0-C5D2E6638337)",
64 		 * including space for escaping, and NUL byte included in sizeof(uuid_string_t).
65 		 */
66 
67 		uuid_string_t uuidstr;
68 		char c_name[2 * MAXCOMLEN + 2 /* () */ + sizeof(uuid_string_t)];
69 		char t_name[2 * MAXCOMLEN + 2 /* () */ + sizeof(uuid_string_t)];
70 
71 		strlcpy(c_name, caller->p_comm, sizeof(c_name));
72 		uuid_unparse_upper(proc_executableuuid_addr(caller), uuidstr);
73 		strlcat(c_name, "(", sizeof(c_name));
74 		strlcat(c_name, uuidstr, sizeof(c_name));
75 		strlcat(c_name, ")", sizeof(c_name));
76 		if (0 != escape_str(c_name, strlen(c_name) + 1, sizeof(c_name))) {
77 			return;
78 		}
79 
80 		strlcpy(t_name, target->p_comm, sizeof(t_name));
81 		uuid_unparse_upper(proc_executableuuid_addr(target), uuidstr);
82 		strlcat(t_name, "(", sizeof(t_name));
83 		strlcat(t_name, uuidstr, sizeof(t_name));
84 		strlcat(t_name, ")", sizeof(t_name));
85 		if (0 != escape_str(t_name, strlen(t_name) + 1, sizeof(t_name))) {
86 			return;
87 		}
88 #if DEBUG
89 		printf("EXTMOD: %s(%d) -> %s(%d)\n",
90 		    c_name,
91 		    proc_pid(caller),
92 		    t_name,
93 		    proc_pid(target));
94 #endif
95 
96 		kern_asl_msg(LOG_DEBUG, "messagetracer",
97 		    5,
98 		    "com.apple.message.domain", "com.apple.kernel.external_modification",                                     /* 0 */
99 		    "com.apple.message.signature", c_name,                                     /* 1 */
100 		    "com.apple.message.signature2", t_name,                                     /* 2 */
101 		    "com.apple.message.result", "noop",                                     /* 3 */
102 		    "com.apple.message.summarize", "YES",                                     /* 4 */
103 		    NULL);
104 	}
105 }
106