2 * Copyright 2012, Samsung Telecommunications of America
3 * Copyright (C) 2014 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * Written by William Roberts <w.roberts@sta.samsung.com>
25 #define LOG_TAG "libaudit"
31 * Waits for an ack from the kernel
33 * The netlink socket fd
35 * The current sequence number were acking on
37 * This function returns 0 on success, else -errno.
39 static int get_ack(int fd, int16_t seq)
42 struct audit_message rep;
44 /* Sanity check, this is an internal interface this shouldn't happen */
49 rc = audit_get_reply(fd, &rep, GET_REPLY_BLOCKING, MSG_PEEK);
54 if (rep.nlh.nlmsg_type == NLMSG_ERROR) {
55 audit_get_reply(fd, &rep, GET_REPLY_BLOCKING, 0);
56 rc = ((struct nlmsgerr *)rep.data)->error;
62 if ((int16_t)rep.nlh.nlmsg_seq != seq) {
63 SLOGW("Expected sequence number between user space and kernel space is out of skew, "
64 "expected %u got %u", seq, rep.nlh.nlmsg_seq);
73 * The netlink socket fd
75 * The type of netlink message
79 * The length of the data in bytes
81 * This function returns a positive sequence number on success, else -errno.
83 static int audit_send(int fd, int type, const void *data, size_t size)
86 static int16_t sequence = 0;
87 struct audit_message req;
88 struct sockaddr_nl addr;
90 memset(&req, 0, sizeof(req));
91 memset(&addr, 0, sizeof(addr));
93 /* We always send netlink messaged */
94 addr.nl_family = AF_NETLINK;
96 /* Set up the netlink headers */
97 req.nlh.nlmsg_type = type;
98 req.nlh.nlmsg_len = NLMSG_SPACE(size);
99 req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
102 * Check for a valid fd, even though sendto would catch this, its easier
103 * to always blindly increment the sequence number
109 /* Ensure the message is not too big */
110 if (NLMSG_SPACE(size) > MAX_AUDIT_MESSAGE_LENGTH) {
111 SLOGE("netlink message is too large");
115 /* Only memcpy in the data if it was specified */
117 memcpy(NLMSG_DATA(&req.nlh), data, size);
121 * Only increment the sequence number on a guarantee
122 * you will send it to the kernel.
124 * Also, the sequence is defined as a u32 in the kernel
125 * struct. Using an int here might not work on 32/64 bit splits. A
126 * signed 64 bit value can overflow a u32..but a u32
127 * might not fit in the response, so we need to use s32.
128 * Which is still kind of hackish since int could be 16 bits
129 * in size. The only safe type to use here is a signed 16
132 req.nlh.nlmsg_seq = ++sequence;
134 /* While failing and its due to interrupts */
136 rc = TEMP_FAILURE_RETRY(sendto(fd, &req, req.nlh.nlmsg_len, 0,
137 (struct sockaddr*) &addr, sizeof(addr)));
139 /* Not all the bytes were sent */
142 SLOGE("Error sending data over the netlink socket: %s", strerror(-errno));
144 } else if ((uint32_t) rc != req.nlh.nlmsg_len) {
149 /* We sent all the bytes, get the ack */
150 rc = get_ack(fd, sequence);
152 /* If the ack failed, return the error, else return the sequence number */
153 rc = (rc == 0) ? (int) sequence : rc;
156 /* Don't let sequence roll to negative */
158 SLOGW("Auditd to Kernel sequence number has rolled over");
165 int audit_set_pid(int fd, uint32_t pid, rep_wait_t wmode)
168 struct audit_message rep;
169 struct audit_status status;
171 memset(&status, 0, sizeof(status));
174 * In order to set the auditd PID we send an audit message over the netlink
175 * socket with the pid field of the status struct set to our current pid,
176 * and the the mask set to AUDIT_STATUS_PID
179 status.mask = AUDIT_STATUS_PID;
181 /* Let the kernel know this pid will be registering for audit events */
182 rc = audit_send(fd, AUDIT_SET, &status, sizeof(status));
184 SLOGE("Could net set pid for audit events, error: %s", strerror(-rc));
189 * In a request where we need to wait for a response, wait for the message
190 * and discard it. This message confirms and sync's us with the kernel.
191 * This daemon is now registered as the audit logger. Only wait if the
192 * wmode is != WAIT_NO
194 if (wmode != WAIT_NO) {
196 * If the daemon dies and restarts the message didn't come back,
197 * so I went to non-blocking and it seemed to fix the bug.
198 * Need to investigate further.
200 audit_get_reply(fd, &rep, GET_REPLY_NONBLOCKING, 0);
208 return socket(PF_NETLINK, SOCK_RAW, NETLINK_AUDIT);
211 int audit_get_reply(int fd, struct audit_message *rep, reply_t block, int peek)
217 struct sockaddr_nl nladdr;
218 socklen_t nladdrlen = sizeof(nladdr);
224 /* Set up the flags for recv from */
225 flags = (block == GET_REPLY_NONBLOCKING) ? MSG_DONTWAIT : 0;
229 * Get the data from the netlink socket but on error we need to be carefull,
230 * the interface shows that EINTR can never be returned, other errors,
231 * however, can be returned.
233 len = TEMP_FAILURE_RETRY(recvfrom(fd, rep, sizeof(*rep), flags,
234 (struct sockaddr*) &nladdr, &nladdrlen));
237 * EAGAIN should be re-tried until success or another error manifests.
241 if (block == GET_REPLY_NONBLOCKING && rc == -EAGAIN) {
242 /* If request is non blocking and errno is EAGAIN, just return 0 */
245 SLOGE("Error receiving from netlink socket, error: %s", strerror(-rc));
249 if (nladdrlen != sizeof(nladdr)) {
250 SLOGE("Protocol fault, error: %s", strerror(EPROTO));
254 /* Make sure the netlink message was not spoof'd */
256 SLOGE("Invalid netlink pid received, expected 0 got: %d", nladdr.nl_pid);
260 /* Check if the reply from the kernel was ok */
261 if (!NLMSG_OK(&rep->nlh, (size_t)len)) {
262 rc = (len == sizeof(*rep)) ? -EFBIG : -EBADE;
263 SLOGE("Bad kernel response %s", strerror(-rc));
269 void audit_close(int fd)
273 SLOGE("Attempting to close invalid fd %d, error: %s", fd, strerror(errno));