1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
/* Key watching facility.
*
* Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public Licence
* as published by the Free Software Foundation; either version
* 2 of the Licence, or (at your option) any later version.
*/
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <poll.h>
#include <getopt.h>
#include <sys/wait.h>
#include "keyutils.h"
#include <limits.h>
#include "keyctl.h"
#include "watch_queue.h"
#define MAX_MESSAGE_COUNT 256
static int consumer_stop;
static pid_t pid_con = -1, pid_cmd = -1;
static key_serial_t session;
static int watch_fd;
static int debug;
static struct watch_notification_filter filter = {
.nr_filters = 0,
.filters = {
/* Reserve a slot */
[0] = {
.type = WATCH_TYPE_KEY_NOTIFY,
},
},
};
static inline bool after_eq(unsigned int a, unsigned int b)
{
return (signed int)(a - b) >= 0;
}
static void consumer_term(int sig)
{
consumer_stop = 1;
}
static void saw_key_change(FILE *log, struct watch_notification *n,
unsigned int len)
{
struct key_notification *k = (struct key_notification *)n;
if (len != sizeof(struct key_notification))
return;
switch (n->subtype) {
case NOTIFY_KEY_INSTANTIATED:
fprintf(log, "%u inst\n", k->key_id);
break;
case NOTIFY_KEY_UPDATED:
fprintf(log, "%u upd\n", k->key_id);
break;
case NOTIFY_KEY_LINKED:
fprintf(log, "%u link %u\n", k->key_id, k->aux);
break;
case NOTIFY_KEY_UNLINKED:
fprintf(log, "%u unlk %u\n", k->key_id, k->aux);
break;
case NOTIFY_KEY_CLEARED:
fprintf(log, "%u clr\n", k->key_id);
break;
case NOTIFY_KEY_REVOKED:
fprintf(log, "%u rev\n", k->key_id);
break;
case NOTIFY_KEY_INVALIDATED:
fprintf(log, "%u inv\n", k->key_id);
break;
case NOTIFY_KEY_SETATTR:
fprintf(log, "%u attr\n", k->key_id);
break;
}
}
/*
* Handle removal notification.
*/
static void saw_removal_notification(FILE *gc, struct watch_notification *n,
unsigned int len)
{
key_serial_t key = 0;
unsigned int wp;
wp = (n->info & WATCH_INFO_ID) >> WATCH_INFO_ID__SHIFT;
if (len >= sizeof(struct watch_notification_removal)) {
struct watch_notification_removal *r = (void *)n;
key = r->id;
}
fprintf(gc, "%u gc\n", key);
if (wp == 1)
exit(0);
}
/*
* Consume and display events.
*/
static __attribute__((noreturn))
int consumer(FILE *log, FILE *gc, int fd)
{
unsigned char buffer[433], *p, *end;
union {
struct watch_notification n;
unsigned char buf1[128];
} n;
ssize_t buf_len;
setlinebuf(log);
setlinebuf(gc);
signal(SIGTERM, consumer_term);
do {
if (!consumer_stop) {
struct pollfd pf[1];
pf[0].fd = fd;
pf[0].events = POLLIN;
pf[0].revents = 0;
if (poll(pf, 1, -1) == -1) {
if (errno == EINTR)
continue;
error("poll");
}
}
buf_len = read(fd, buffer, sizeof(buffer));
if (buf_len == -1) {
perror("read");
exit(1);
}
if (buf_len == 0) {
printf("-- END --\n");
exit(0);
}
if (buf_len > sizeof(buffer)) {
fprintf(stderr, "Read buffer overrun: %zd\n", buf_len);
exit(4);
}
if (debug)
fprintf(stderr, "read() = %zd\n", buf_len);
p = buffer;
end = buffer + buf_len;
while (p < end) {
size_t largest, len;
largest = end - p;
if (largest > 128)
largest = 128;
if (largest < sizeof(struct watch_notification)) {
fprintf(stderr, "Short message header: %zu\n", largest);
exit(4);
}
memcpy(&n, p, largest);
if (debug)
fprintf(stderr, "NOTIFY[%03tx]: ty=%06x sy=%02x i=%08x\n",
p - buffer, n.n.type, n.n.subtype, n.n.info);
len = n.n.info & WATCH_INFO_LENGTH;
if (len < sizeof(n.n) || len > largest) {
fprintf(stderr, "Bad message length: %zu/%zu\n", len, largest);
exit(1);
}
switch (n.n.type) {
case WATCH_TYPE_META:
switch (n.n.subtype) {
case WATCH_META_REMOVAL_NOTIFICATION:
saw_removal_notification(gc, &n.n, len);
break;
case WATCH_META_LOSS_NOTIFICATION:
fprintf(log, "-- LOSS --\n");
break;
default:
if (debug)
fprintf(stderr, "other meta record\n");
break;
}
break;
case WATCH_TYPE_KEY_NOTIFY:
saw_key_change(log, &n.n, len);
break;
default:
if (debug)
fprintf(stderr, "other type\n");
break;
}
p += len;
}
} while (!consumer_stop);
fprintf(log, "Monitoring terminated\n");
if (gc != log)
fprintf(gc, "Monitoring terminated\n");
exit(0);
}
/*
* Open the watch device and allocate a buffer.
*/
static int open_watch(void)
{
int pipefd[2], fd;
if (pipe2(pipefd, O_NOTIFICATION_PIPE | O_NONBLOCK) == -1)
error("pipe2");
fd = pipefd[0];
if (ioctl(fd, IOC_WATCH_QUEUE_SET_SIZE, MAX_MESSAGE_COUNT) == -1)
error("/dev/watch_queue(size)");
if (filter.nr_filters &&
ioctl(fd, IOC_WATCH_QUEUE_SET_FILTER, &filter) == -1)
error("/dev/watch_queue(filter)");
return fd;
}
/*
* Parse a filter character representation into a subtype number.
*/
static bool parse_subtype(struct watch_notification_type_filter *t, char filter)
{
static const char filter_mapping[] =
"i" /* 0 NOTIFY_KEY_INSTANTIATED */
"p" /* 1 NOTIFY_KEY_UPDATED */
"l" /* 2 NOTIFY_KEY_LINKED */
"n" /* 3 NOTIFY_KEY_UNLINKED */
"c" /* 4 NOTIFY_KEY_CLEARED */
"r" /* 5 NOTIFY_KEY_REVOKED */
"v" /* 6 NOTIFY_KEY_INVALIDATED */
"s" /* 7 NOTIFY_KEY_SETATTR */
;
const char *p;
unsigned int st_bits;
unsigned int st_index;
unsigned int st_bit;
int subtype;
p = strchr(filter_mapping, filter);
if (!p)
return false;
subtype = p - filter_mapping;
st_bits = sizeof(t->subtype_filter[0]) * 8;
st_index = subtype / st_bits;
st_bit = 1U << (subtype % st_bits);
t->subtype_filter[st_index] |= st_bit;
return true;
}
/*
* Parse filters.
*/
static void parse_watch_filter(char *str)
{
struct watch_notification_filter *f = &filter;
struct watch_notification_type_filter *t0 = &f->filters[0];
f->nr_filters = 1;
t0->type = WATCH_TYPE_KEY_NOTIFY;
for (; *str; str++) {
if (parse_subtype(t0, *str))
continue;
fprintf(stderr, "Unknown filter character '%c'\n", *str);
exit(2);
}
}
/*
* Watch a key or keyring for changes.
*/
void act_keyctl_watch(int argc, char *argv[])
{
key_serial_t key;
int wfd, opt;
while (opt = getopt(argc, argv, "f:"),
opt != -1) {
switch (opt) {
case 'f':
parse_watch_filter(optarg);
break;
default:
fprintf(stderr, "Unknown option\n");
exit(2);
}
}
argv += optind;
argc -= optind;
if (argc != 1)
format();
key = get_key_id(argv[0]);
wfd = open_watch();
if (keyctl_watch_key(key, wfd, 0x01) == -1)
error("keyctl_watch_key");
consumer(stdout, stdout, wfd);
}
/*
* Add a watch on a key to the monitor created by watch_session.
*/
void act_keyctl_watch_add(int argc, char *argv[])
{
key_serial_t key;
int fd;
if (argc != 3)
format();
fd = atoi(argv[1]);
key = get_key_id(argv[2]);
if (keyctl_watch_key(key, fd, 0x02) == -1)
error("keyctl_watch_key");
exit(0);
}
/*
* Remove a watch on a key from the monitor created by watch_session.
*/
void act_keyctl_watch_rm(int argc, char *argv[])
{
key_serial_t key;
int fd;
if (argc != 3)
format();
fd = atoi(argv[1]);
key = get_key_id(argv[2]);
if (keyctl_watch_key(key, fd, -1) == -1)
error("keyctl_watch_key");
exit(0);
}
static void exit_cleanup(void)
{
pid_t me = getpid();
int w;
if (me != pid_cmd && me != pid_con) {
keyctl_watch_key(session, watch_fd, -1);
if (pid_cmd != -1) {
kill(pid_cmd, SIGTERM);
waitpid(pid_cmd, &w, 0);
}
if (pid_con != -1) {
kill(pid_con, SIGTERM);
waitpid(pid_con, &w, 0);
}
}
}
static void run_command(int argc, char *argv[], int wfd)
{
char buf[16];
pid_cmd = fork();
if (pid_cmd == -1)
error("fork");
if (pid_cmd != 0)
return;
pid_cmd = -1;
pid_con = -1;
sprintf(buf, "%u", wfd);
setenv("KEYCTL_WATCH_FD", buf, true);
/* run the standard shell if no arguments */
if (argc == 0) {
const char *q = getenv("SHELL");
if (!q)
q = "/bin/sh";
execl(q, q, NULL);
error(q);
}
/* run the command specified */
execvp(argv[0], argv);
error(argv[0]);
}
/*
* Open a logfiles.
*/
static FILE *open_logfile(const char *logfile)
{
unsigned int flags;
FILE *log;
int lfd;
log = fopen(logfile, "a");
if (!log)
error(logfile);
lfd = fileno(log);
flags = fcntl(lfd, F_GETFD);
if (flags == -1)
error("F_GETFD");
if (fcntl(lfd, F_SETFD, flags | FD_CLOEXEC) == -1)
error("F_SETFD");
return log;
}
/*
* Set up a new session keyring with a monitor that is exposed on an explicit
* file descriptor in the program that it starts.
*/
void act_keyctl_watch_session(int argc, char *argv[])
{
const char *session_name = NULL;
const char *logfile, *gcfile, *target_fd;
unsigned int flags;
pid_t pid;
FILE *log, *gc;
int wfd, tfd, opt, w, e = 0, e2 = 0;
while (opt = getopt(argc, argv, "+df:n:"),
opt != -1) {
switch (opt) {
case 'd':
debug = 1;
break;
case 'f':
parse_watch_filter(optarg);
break;
case 'n':
session_name = optarg;
break;
default:
fprintf(stderr, "Unknown option\n");
exit(2);
}
}
argv += optind;
argc -= optind;
if (argc < 4)
format();
logfile = argv[0];
gcfile = argv[1];
target_fd = argv[2];
tfd = atoi(target_fd);
if (tfd < 3 || tfd > 9) {
fprintf(stderr, "The target fd must be between 3 and 9\n");
exit(2);
}
wfd = open_watch();
if (wfd != tfd) {
if (dup2(wfd, tfd) == -1)
error("dup2");
close(wfd);
wfd = tfd;
}
watch_fd = wfd;
atexit(exit_cleanup);
/* We want the fd to be inherited across a fork. */
flags = fcntl(wfd, F_GETFD);
if (flags == -1)
error("F_GETFD");
if (fcntl(wfd, F_SETFD, flags & ~FD_CLOEXEC) == -1)
error("F_SETFD");
log = open_logfile(logfile);
gc = open_logfile(gcfile);
pid_con = fork();
if (pid_con == -1)
error("fork");
if (pid_con == 0) {
pid_cmd = -1;
pid_con = -1;
consumer(log, gc, wfd);
}
/* Create a new session keyring and watch it. */
session = keyctl_join_session_keyring(session_name);
if (session == -1)
error("keyctl_join_session_keyring");
if (keyctl_watch_key(session, wfd, 0x01) == -1)
error("keyctl_watch_key/session");
fprintf(stderr, "Joined session keyring: %d\n", session);
/* Start the command and then wait for it to finish and the
* notification consumer to clean up.
*/
run_command(argc - 3, argv + 3, wfd);
close(wfd);
wfd = -1;
while (pid = wait(&w),
pid != -1) {
if (pid == pid_cmd) {
if (pid_con != -1)
kill(pid_con, SIGTERM);
if (WIFEXITED(w)) {
e2 = WEXITSTATUS(w);
pid_cmd = -1;
} else if (WIFSIGNALED(w)) {
e2 = WTERMSIG(w) + 128;
pid_cmd = -1;
} else if (WIFSTOPPED(w)) {
raise(WSTOPSIG(w));
}
} else if (pid == pid_con) {
if (pid_cmd != -1)
kill(pid_cmd, SIGTERM);
if (WIFEXITED(w)) {
e = WEXITSTATUS(w);
pid_con = -1;
} else if (WIFSIGNALED(w)) {
e = WTERMSIG(w) + 128;
pid_con = -1;
}
}
}
if (e == 0)
e = e2;
exit(e);
}
/*
* Wait for monitoring to synchronise.
*/
void act_keyctl_watch_sync(int argc, char *argv[])
{
long ret;
int wfd, count;
if (argc != 2)
format();
wfd = atoi(argv[1]);
ret = ioctl(wfd, PIPE_IOC_SYNC);
if (ret == 0)
exit(0);
if (ret == -1 && errno != ENOTTY)
error("ioctl(PIPE_IOC_SYNC)");
for (;;) {
ret = ioctl(wfd, FIONREAD, &count);
if (ret == -1)
error("ioctl(FIONREAD)");
if (count == 0)
break;
usleep(200 * 1000);
}
exit(0);
}
|