File: kmview.c

package info (click to toggle)
umview 0.8.2-1.2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,472 kB
  • sloc: ansic: 67,309; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (464 lines) | stat: -rw-r--r-- 11,469 bytes parent folder | download | duplicates (5)
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
/*   This is part of km-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   kmview.c: main
 *   
 *   Copyright 2007 Renzo Davoli University of Bologna - Italy
 *   Based on umview: 2005 Renzo Davoli
 *   Modified 2005 Ludovico Gardenghi, Andrea Gasparini, Andrea Seraghiti
 *   
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2, as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *   $Id: kmview.c 968 2011-08-03 10:16:26Z rd235 $
 *
 */   
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#ifdef OLDVIRSC
#include <linux/sysctl.h>
#endif
#include <config.h>

#ifndef _VIEWOS_KM
#define _VIEWOS_KM
#endif

#include "defs.h"
#include "kmview-main.h"
#include "capture_km.h"
#include "capture_nested.h"
#include "sctab.h"
#include "services.h"
#include "um_select.h"
#include "um_services.h"
#include "mainpoll.h"
#include "gdebug.h"
#include "loginshell.h"

#ifdef GDEBUG_ENABLED
#	define OPTSTRING "+p:f:o:hvxqV:us"
#else
#	define OPTSTRING "+p:f:hvxqV:us"
#endif
#define KMVIEW_USER_NESTING

int _umview_version = 2; /* modules interface version id.
										modules can test to be compatible with
										um-viewos kernel*/
unsigned int quiet = 0;
unsigned int printk_current_level = PRINTK_STARTUP_LEVEL;
unsigned int secure = 0;
static char *viewname;

extern int nprocs;

struct prelist {
	char *module;
	struct prelist *next;
};

/* module preload list */
static struct prelist *prehead=NULL;

/* add a module for pre-loading */
static void preadd(struct prelist **head,char *module)
{
	struct prelist *new=malloc(sizeof(struct prelist));
	assert(new);
	new->module=module;
	new->next=*head;
	*head=new;
}

#ifdef KMVIEW_USER_NESTING
/* virtual syscall for the underlying umview */
#ifdef OLDVIRSC
static long int_virnsyscall(long virscno,int n,long arg1,long arg2,long arg3,long arg4,long arg5,long arg6) {
	struct __sysctl_args scarg;
	long args[6]={arg1,arg2,arg3,arg4,arg5,arg6};
	scarg.name=NULL;
	scarg.nlen=virscno;
	scarg.oldval=NULL;
	scarg.oldlenp=NULL;
	scarg.newval=args;
	scarg.newlen=n;
	return native_syscall(__NR__sysctl,&scarg);
}
#else
static long int_virnsyscall(long virscno,int n,long arg1,long arg2,long arg3,long arg4,long arg5,long arg6) {
	long args[6]={arg1,arg2,arg3,arg4,arg5,arg6};
	return native_syscall(__NR_pivot_root,NULL,n,virscno,args);
}
#endif
#endif

/* preload of modules */
static int do_preload(struct prelist *head)
{
	if (head != NULL) {
		int rv=do_preload(head->next);
		if (add_service(head->module,0) < 0) {
			printk("module preload %s",strerror(errno));
			return -1 ;
		} else
			return rv;
		free(head);
	} else
		return 0;
}


static void do_set_viewname(char *viewname)
{
	if (viewname) {
		pcb_setviewname(get_pcb(),viewname);
		free(viewname);
	}
}

#ifdef KMVIEW_USER_NESTING
/* preload for nexted umview (it is a burst of um_add_module) */
static int do_preload_recursive(struct prelist *head)
{
	if (head != NULL) {
		do_preload_recursive(head->next);
		int_virnsyscall(__NR_UM_SERVICE,3,ADD_SERVICE,(long)head->module,0,0,0,0);
		free(head);
		return 0;
	} else
		return 0;
}

static void do_set_viewname_recursive(char *viewname)
{
	if (viewname) {
		int_virnsyscall(__NR_UM_SERVICE,2,VIEWOS_SETVIEWNAME,(long)viewname,0,0,0,0);
	}
}
#endif

static void version(int verbose)
{
	fprintf(stderr, "%s %s\n", KMVIEW_NAME, KMVIEW_VERSION);

	if (verbose)
		fprintf(stderr, "%s\n", KMVIEW_DESC);

	fprintf(stderr, "Copyright (C) %s\n", KMVIEW_COPYRIGHT);
	
	if (verbose)
		fprintf(stderr, "Development team:\n%s\n", KMVIEW_TEAM);

	fprintf(stderr, "%s\n\n", KMVIEW_URL);
	return;
}

static void usage(char *s)
{
	version(0);
	
	fprintf(stderr, "Usage: %s [OPTION] ... command [args]\n"
			"  -h, --help                print this help message\n"
			"  -v, --version             show version information\n"
			"  -q, --quiet               suppress some additional output\n"
			"  -V name, --viewname name  set the view name\n"
			"  -f file, --rc file        set rc file\n"
			"  -p file, --preload file   load plugin named `file' (must be a .so)\n"
#ifdef GDEBUG_ENABLED
			"  -o file, --output file    send debug messages to file instead of stderr\n"
#endif
			"  -x, --nonesting           do not permit module nesting\n"
			"  -u, --userrecursion       recursive invocation on the existing hypervisor\n"
			"  -s, --secure		           force permissions and capabilities\n",
			s);
	exit(0);
}

static struct option long_options[] = {
	{"preload",1,0,'p'},
#ifdef GDEBUG_ENABLED
	{"output",1,0,'o'},
#endif
	{"version",0,0,'v'},
	{"quiet",0,0,'q'},
	{"viewname",1,0,'V'},
	{"help",0,0,'h'},
	{"nonesting",0,0,'x'},
	{"userrecursion",0,0,'u'},
	{"secure",0,0,'s'},
	{0,0,0,0}
};

/* pure_libc loading (by reloading the entire kmview) */
static void load_it_again(int argc,char *argv[],int login)
{
	int nesting=1;
	optind=1;
	while (1) {
		int c;
		int option_index = 0;
		/* some options must be parsed before reloading */
		c = getopt_long(argc, argv, OPTSTRING, long_options, &option_index);

		if (c == -1) break;
		switch (c) {
			case 'h':
				usage(basename(argv[0]));
				break;
			case 'v':
				version(1);
				exit(0);
				break;
			case 'x': /* do not use pure_libc */
				nesting=0;
				break;
		}
	}
	if (nesting) {
		char *path;
		void *handle;
		/* does pure_libc exist ? */
		if ((handle=dlopen("libpurelibc.so",RTLD_LAZY))!=NULL) {
			dlclose(handle);
			/* get the executable from /proc */
			asprintf(&path,"/proc/%d/exe",getpid());
			/* preload the pure_libc library */
			setenv("LD_PRELOAD","libpurelibc.so",1);
			/* reload the executable with a leading - */
			if (login)
				argv[0]="--kmview-login";
			else
				argv[0]="--kmview";
			execv(path,argv);
			/* useless cleanup */
			free(path);
		}
	}
}

#ifdef KMVIEW_USER_NESTING

/* recursive kmview invocation (umview started inside a umview machine) */
static void kmview_recursive(int argc,char *argv[])
{
	char *rcfile=NULL;
	if (argc < 2)
	{
		usage(argv[0]);
		exit(1);
	}

	optind=1;
	while (1) {
		int c;
		int option_index = 0;
		c = getopt_long(argc, argv, OPTSTRING, long_options, &option_index);
		if (c == -1) break;
		switch (c) {
			case 'h':
				usage(argv[0]);
				break;
			case 'q':
				quiet = 1;
				break;
			case 'v':
				version(1);
				exit(0);
				break;
			case 'f':
				rcfile=strdup(optarg);
				break;
			case 'V':
				viewname=strdup(optarg);
				break;
			case 'p': 
				preadd(&prehead,optarg);
				break;
		}
	}
	if (!quiet)
		fprintf(stderr,"KMView: nested invocation\n\n");
	if (rcfile==NULL)
		asprintf(&rcfile,"%s/%s",getenv("HOME"),".viewosrc");
	capture_execrc("/etc/viewosrc","nested");
	if (rcfile != NULL && *rcfile != 0)
		capture_execrc(rcfile,"nested");
	do_preload_recursive(prehead);
	do_set_viewname_recursive(viewname);
	/* exec the process */
	execvp(*(argv+optind),argv+optind);
	exit(-1);
}

static int test_recursion(int argc,char *argv[])
{
	int userrecursion=0;
	optind=1;
	while (1) {
		int c;
		int option_index = 0;
		c = getopt_long(argc, argv, OPTSTRING, long_options, &option_index);
		if (c == -1) break;
		switch (c) {
			case 'u':
				userrecursion = 1;
				break;
		}
	}
	return userrecursion;
}

#endif

static void root_process_init()
{
	capture_nested_init();
	setenv("_INSIDE_VIEWOS_MODULE","",1);
	do_preload(prehead);
	do_set_viewname(viewname);
}

#define PROGNAME "kmview"
#include<errno.h>
/* KMVIEW MAIN PROGRAM */
int main(int argc,char *argv[])
{
	char *rcfile=NULL;
	/*loginshell is true if this execution is driven by a login shell
		(maybe indirectly: main restarted from /etc/viewospasswd or
		reloaded for purelibc) */
	int loginshell=isloginshell(argv[0]);
	/* login shell? (directly from /etc/passwd) */
	if (argc == 1 && argv[0][0] == '-' && argv[0][1] != '-') 
		loginshell_view();
	if (argc < 2) /* NO ARGS */
	{
		usage(PROGNAME);
		exit(1);
	}

	/* try to set the priority to -11 provided umview has been installed
	 * setuid. it is effectiveless elsewhere */
	r_setpriority(PRIO_PROCESS,0,-11);
	/* if it was setuid, return back to the user status immediately,
	 * for safety! */
	r_setuid(getuid());
	/* Check these cases only when *not* reloaded for purelibc */
	if (strncmp(argv[0],"--kmview",8)!=0) {
		/* if this is a nested invocation of umview, notify the umview monitor
		 * and execute the process, 
		 * try the nested invocation notifying virtual syscall, 
		 * if it succeeded it is actually a nested invocation,
		 * otherwise nobody is notified and the call fails*/
#ifdef KMVIEW_USER_NESTING
		if (test_recursion(argc,argv)) {
			if (int_virnsyscall(__NR_UM_SERVICE,1,RECURSIVE_VIEWOS,0,0,0,0,0) >= 0)
				kmview_recursive(argc,argv);	/* do not return!*/
		}
#endif
		/* umview loads itself twice if there is pure_libc, to trace module 
		 * generated syscalls, this condition manages the first call */
		load_it_again(argc,argv,loginshell);	/* do not return (when purelibc and not -x)!*/
	}

	/* does this kernel provide pselect? */
	/*has_pselect=has_pselect_test();*/
	optind=1;
	/* set up the scdtab */
	scdtab_init();
	/* test the ptrace support */
	/* option management */
	while (1) {
		int c;
		int option_index = 0;
		c = getopt_long(argc, argv, OPTSTRING, long_options, &option_index);
		if (c == -1) break;
		switch (c) {
			case 'h': /* help */
				usage(PROGNAME);
				break;
			case 'v': /* version */
				version(1);
				exit(0);
				break;
			case 'V':
				viewname=strdup(optarg);
				break;
			/* XXX todo preload */
			case 'p': /* module preload, here the module requests are just added to
			             a data structure */
				preadd(&prehead,optarg);
				break;
			case 'f':
				rcfile=strdup(optarg);
				break;
			case 'q':
				quiet = 1;
				break;
			case 's':
				secure = 1;
				break;
#ifdef GDEBUG_ENABLED
			case 'o': /* debugging output file redirection */ { 
						if (optarg==NULL){
							fprintf(stderr, "%s: must specify an argument after -o\n",argv[0]);
							break;
						}
						gdebug_set_ofile(optarg);
					 }
					 break;
#endif
		}
	}
	
	if (!quiet) {
		fprintf(stderr, "Kmview: %s\nver: %s\n\n",KMVIEW_DESC,PACKAGE_VERSION);
	}

	if (rcfile==NULL && !isloginshell(argv[0]))
		asprintf(&rcfile,"%s/%s",getenv("HOME"),".viewosrc");

	sigset_t unblockchild;
	sigprocmask(SIG_BLOCK,NULL,&unblockchild);
	pcb_inits(0);

	if (quiet) {
		setenv("_VIEWOS_QUIET","1",1);
		printk_current_level = PRINTK_QUIET_LEVEL; /* warnings or errors only */
	}

	if (capture_main(argv+optind,root_process_init,rcfile) < 0) {
		printk("Kmview: kernel module not loaded\n");
		exit(1);
	}
	mp_add(kmviewfd,POLLIN,tracehand,NULL,1);
	GDEBUG(3,"ENTERING %d ",kmviewfd);
	do {
		mp_ppoll(&unblockchild);
	} while (nprocs>0);
	pcb_finis(0);
	return first_child_exit_status;
}