2005-05-30 22:02:31 +04:00
|
|
|
/* $NetBSD: veriexecctl.c,v 1.12 2005/05/30 18:02:31 elad Exp $ */
|
2002-11-23 13:52:49 +03:00
|
|
|
|
|
|
|
/*-
|
2005-04-20 17:44:45 +04:00
|
|
|
* Copyright 2005 Elad Efrat <elad@bsd.org.il>
|
|
|
|
* Copyright 2005 Brett Lymn <blymn@netbsd.org>
|
|
|
|
*
|
2002-11-23 13:52:49 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code has been donated to The NetBSD Foundation by the Author.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software withough specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-04-20 17:44:45 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/verified_exec.h>
|
2002-11-23 13:52:49 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2002-12-06 05:26:14 +03:00
|
|
|
#include <stdlib.h>
|
2005-04-20 17:44:45 +04:00
|
|
|
#include <string.h>
|
2002-11-23 13:52:49 +03:00
|
|
|
#include <fcntl.h>
|
2005-04-20 17:44:45 +04:00
|
|
|
#include <unistd.h>
|
2004-03-06 14:57:14 +03:00
|
|
|
#include <err.h>
|
2005-04-20 17:44:45 +04:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "veriexecctl.h"
|
|
|
|
|
|
|
|
#define VERIEXEC_DEVICE "/dev/veriexec"
|
2004-03-06 14:57:14 +03:00
|
|
|
|
2005-04-20 17:44:45 +04:00
|
|
|
extern struct veriexec_params params; /* in veriexecctl_parse.y */
|
|
|
|
extern char *filename; /* in veriexecctl_conf.l */
|
2005-05-21 00:06:34 +04:00
|
|
|
int gfd, verbose = 0, phase;
|
2005-04-21 16:45:12 +04:00
|
|
|
size_t line;
|
2002-11-23 13:52:49 +03:00
|
|
|
|
2005-04-20 17:44:45 +04:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
2005-04-21 16:45:12 +04:00
|
|
|
static FILE *openlock(const char *);
|
|
|
|
static void phase1_preload(void);
|
|
|
|
static int fingerprint_load(char*);
|
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
2005-04-21 15:21:58 +04:00
|
|
|
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
static FILE *
|
2005-04-20 17:44:45 +04:00
|
|
|
openlock(const char *path)
|
2002-11-23 13:52:49 +03:00
|
|
|
{
|
2005-04-21 15:21:58 +04:00
|
|
|
int lfd;
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
if ((lfd = open(path, O_RDONLY|O_EXLOCK, 0)) == -1)
|
|
|
|
return NULL;
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
return fdopen(lfd, "r");
|
2005-04-20 17:44:45 +04:00
|
|
|
}
|
|
|
|
|
2005-05-21 00:06:34 +04:00
|
|
|
struct veriexec_up *
|
2005-04-20 17:44:45 +04:00
|
|
|
dev_lookup(dev_t d)
|
|
|
|
{
|
2005-05-21 00:06:34 +04:00
|
|
|
struct veriexec_up *p;
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
CIRCLEQ_FOREACH(p, ¶ms_list, vu_list)
|
2005-04-20 17:44:45 +04:00
|
|
|
if (p->vu_param.dev == d)
|
|
|
|
return (p);
|
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
return NULL;
|
2005-04-20 17:44:45 +04:00
|
|
|
}
|
|
|
|
|
2005-05-21 00:06:34 +04:00
|
|
|
struct veriexec_up *
|
2005-04-20 17:44:45 +04:00
|
|
|
dev_add(dev_t d)
|
|
|
|
{
|
2005-05-21 00:06:34 +04:00
|
|
|
struct veriexec_up *up;
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
if ((up = calloc((size_t)1, sizeof(*up))) == NULL)
|
2005-04-20 17:44:45 +04:00
|
|
|
err(1, "No memory");
|
|
|
|
|
|
|
|
up->vu_param.dev = d;
|
|
|
|
up->vu_param.hash_size = 1;
|
|
|
|
|
|
|
|
CIRCLEQ_INSERT_TAIL(¶ms_list, up, vu_list);
|
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
return up;
|
2005-04-20 17:44:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load all devices, get rid of the list. */
|
2005-04-21 16:45:12 +04:00
|
|
|
static void
|
2005-04-20 17:44:45 +04:00
|
|
|
phase1_preload(void)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
printf("Phase 1: Calculating hash table sizes:\n");
|
|
|
|
|
|
|
|
while (!CIRCLEQ_EMPTY(¶ms_list)) {
|
2005-05-21 00:06:34 +04:00
|
|
|
struct veriexec_up *vup;
|
2005-04-20 17:44:45 +04:00
|
|
|
|
|
|
|
vup = CIRCLEQ_FIRST(¶ms_list);
|
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
if (ioctl(gfd, VERIEXEC_TABLESIZE, &(vup->vu_param)) == -1)
|
|
|
|
err(1, "Error in phase 1: Can't "
|
|
|
|
"set hash table size for device %d",
|
|
|
|
vup->vu_param.dev);
|
2005-04-20 17:44:45 +04:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
printf(" => Hash table sizing successful for device "
|
2005-04-21 15:21:58 +04:00
|
|
|
"%d. (%zu entries)\n", vup->vu_param.dev,
|
2005-04-20 17:44:45 +04:00
|
|
|
vup->vu_param.hash_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
CIRCLEQ_REMOVE(¶ms_list, vup, vu_list);
|
|
|
|
free(vup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the fingerprint. Assumes that the fingerprint pseudo-device is
|
2005-04-21 15:21:58 +04:00
|
|
|
* opened and the file handle is in gfd.
|
2005-04-20 17:44:45 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
phase2_load(void)
|
|
|
|
{
|
2005-04-21 16:45:12 +04:00
|
|
|
if (ioctl(gfd, VERIEXEC_LOAD, ¶ms) == -1)
|
2005-05-30 22:02:31 +04:00
|
|
|
warn("Cannot load params from `%s'", params.file);
|
2005-04-20 17:44:45 +04:00
|
|
|
free(params.fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fingerprint load handling.
|
|
|
|
*/
|
2005-04-21 16:45:12 +04:00
|
|
|
static int
|
2005-04-21 15:21:58 +04:00
|
|
|
fingerprint_load(char *ifile)
|
2005-04-20 17:44:45 +04:00
|
|
|
{
|
|
|
|
CIRCLEQ_INIT(¶ms_list);
|
2002-11-23 13:52:49 +03:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
if ((yyin = openlock(ifile)) == NULL)
|
|
|
|
err(1, "Cannot open `%s'", ifile);
|
2002-11-23 13:52:49 +03:00
|
|
|
|
2005-04-20 17:44:45 +04:00
|
|
|
/*
|
|
|
|
* Phase 1: Scan all config files, creating the list of devices
|
|
|
|
* we have fingerprinted files on, and the amount of
|
|
|
|
* files per device. Lock all files to maintain sync.
|
|
|
|
*/
|
|
|
|
phase = 1;
|
|
|
|
|
|
|
|
if (verbose) {
|
2005-04-21 16:45:12 +04:00
|
|
|
(void)printf("Phase 1: Building hash table information:\n");
|
|
|
|
(void)printf("=> Parsing \"%s\"\n", ifile);
|
2005-04-20 17:44:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
yyparse();
|
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
phase1_preload();
|
2005-04-20 17:44:45 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Phase 2: After we have a circular queue containing all the
|
|
|
|
* devices we care about and the sizes for the hash
|
|
|
|
* tables, do a rescan, this time actually loading the
|
|
|
|
* file data.
|
|
|
|
*/
|
|
|
|
rewind(yyin);
|
|
|
|
phase = 2;
|
|
|
|
if (verbose) {
|
2005-04-21 16:45:12 +04:00
|
|
|
(void)printf("Phase 2: Loading per-file fingerprints.\n");
|
|
|
|
(void)printf("=> Parsing \"%s\"\n", ifile);
|
2002-11-23 13:52:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
yyparse();
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
(void)fclose(yyin);
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2005-05-20 23:52:52 +04:00
|
|
|
(void)fprintf(stderr, "Usage: %s [-v] [load <signature_file>] ",
|
|
|
|
getprogname());
|
2005-04-21 16:45:12 +04:00
|
|
|
exit(1);
|
2005-04-20 17:44:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
setprogname(argv[0]);
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
while ((c = getopt(argc, argv, "v")) != -1)
|
2005-04-20 17:44:45 +04:00
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-04-21 16:45:12 +04:00
|
|
|
usage();
|
2005-04-20 17:44:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
if ((gfd = open(VERIEXEC_DEVICE, O_RDWR, 0)) == -1)
|
|
|
|
err(1, "Cannot open `%s'", VERIEXEC_DEVICE);
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-05-20 23:52:52 +04:00
|
|
|
/*
|
|
|
|
* Handle the different commands we can do.
|
|
|
|
*/
|
2005-04-21 16:45:12 +04:00
|
|
|
if (argc == 2 && strcasecmp(argv[0], "load") == 0) {
|
2005-04-20 17:44:45 +04:00
|
|
|
line = 0;
|
|
|
|
filename = argv[1];
|
|
|
|
fingerprint_load(argv[1]);
|
2005-04-21 16:45:12 +04:00
|
|
|
} else
|
|
|
|
usage();
|
2005-04-20 17:44:45 +04:00
|
|
|
|
2005-04-21 16:45:12 +04:00
|
|
|
(void)close(gfd);
|
|
|
|
return 0;
|
2002-11-23 13:52:49 +03:00
|
|
|
}
|