File: sfs_fs.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (473 lines) | stat: -rw-r--r-- 10,072 bytes parent folder | download
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
/*
 * Copyright (c) 1997,1999 The University of Utah and the Flux Group. 
 * All rights reserved. 
 * 
 * Contributed by the Computer Security Research division,
 * INFOSEC Research and Technology Office, NSA.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

#include "sfs.h"

static struct oskit_filesystem_ops fs_ops;
static struct oskit_comsid_ops sid_ops;
static struct oskit_filesystem_secure_ops sfs_ops;

oskit_error_t 
sfilesystem_create(oskit_dir_t * root,
		   oskit_security_id_t sid,
		   oskit_services_t * osenv,
		   oskit_filepsid_t * psid,
		   oskit_avc_t * avc,
		   oskit_security_t * security,
		   struct sfilesystem ** out_sfs)
{
	oskit_osenv_log_t *log;
	oskit_osenv_mem_t *mem;
	struct sfilesystem *sfs;


	oskit_services_lookup_first(osenv, &oskit_osenv_log_iid, 
				    (void **) &log);
	if (!log)
		return OSKIT_EINVAL;

	oskit_services_lookup_first(osenv, &oskit_osenv_mem_iid, 
				    (void **) &mem);
	if (!mem)
		return OSKIT_EINVAL;

	sfs = oskit_osenv_mem_alloc(mem, sizeof(struct sfilesystem), 0, 0);
	if (!sfs)
		return OSKIT_ENOMEM;

	sfs->fsi.ops = &fs_ops;
	sfs->sidi.ops = &sid_ops;
	sfs->sfsi.ops = &sfs_ops;
	sfs->count = 1;
	sfs->sid = sid;
	sfs->mem = mem;
	oskit_osenv_mem_addref(mem);
	sfs->log = log;
	oskit_osenv_log_addref(log);	
	sfs->psid = psid;
	oskit_filepsid_addref(psid);
	sfs->avc = avc;
	oskit_avc_addref(avc);
	sfs->security = security;
	oskit_security_addref(security);
	sfs->root = root;
	oskit_dir_addref(root);
	sfs->fs = NULL;
	oskit_dir_getfs(root, &sfs->fs);
	sfs->ino2sf = hashtab_create(mem);
	if (!sfs->ino2sf) {
		if (sfs->fs)
			oskit_filesystem_release(sfs->fs);
		oskit_dir_release(root);
		oskit_security_release(security);
		oskit_avc_release(avc);
		oskit_filepsid_release(psid);
		oskit_osenv_log_release(log);
		oskit_osenv_mem_free(mem, sfs, 0, sizeof(struct sfilesystem));
		oskit_osenv_mem_release(mem);
		return OSKIT_ENOMEM;
	}

	*out_sfs = sfs;

	return 0;
}


/*
 * oskit_filesystem methods
 */

static OSKIT_COMDECL 
filesystem_query(oskit_filesystem_t * f,
		 const struct oskit_guid * iid,
		 void **out_ihandle)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) f;

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_filesystem_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &sfs->fsi;
		++sfs->count;
		return 0;
	}
	if (memcmp(iid, &oskit_comsid_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &sfs->sidi;
		++sfs->count;
		return 0;
	}
	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}


static OSKIT_COMDECL_U 
filesystem_addref(oskit_filesystem_t * f)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) f;

	return ++sfs->count;
}


static OSKIT_COMDECL_U 
filesystem_release(oskit_filesystem_t * f)
{
	struct sfilesystem *sfs;
	oskit_osenv_mem_t *mem;
	unsigned        newcount;

	sfs = (struct sfilesystem *) f;

	newcount = --sfs->count;
	if (newcount == 0) {
		if (sfs->fs)
			oskit_filesystem_release(sfs->fs);
		oskit_dir_release(sfs->root);
		oskit_security_release(sfs->security);
		oskit_avc_release(sfs->avc);
		oskit_filepsid_release(sfs->psid);
		oskit_osenv_log_release(sfs->log);
		hashtab_destroy(sfs->ino2sf);	
		mem = sfs->mem;
		oskit_osenv_mem_free(mem, sfs, 0, sizeof(struct sfilesystem));
		oskit_osenv_mem_release(mem);
	}
	return newcount;
}


static OSKIT_COMDECL 
filesystem_statfs(oskit_filesystem_t * f,
		  oskit_statfs_t * out_stats)
{
	struct sfilesystem *sfs = (struct sfilesystem *) f;
	oskit_security_id_t csid;
	oskit_error_t   rc;


	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FS_HAS_PERM(csid, sfs, GETATTR);
	if (rc)
		return rc;

	return oskit_filesystem_statfs(sfs->fs, out_stats);
}


static OSKIT_COMDECL 
filesystem_sync(oskit_filesystem_t * f,
		oskit_bool_t wait)
{
	struct sfilesystem *sfs = (struct sfilesystem *) f;
	oskit_security_id_t csid;
	oskit_error_t   rc;


	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FS_HAS_PERM(csid, sfs, SYNC);
	if (rc)
		return rc;

	return oskit_filesystem_sync(sfs->fs, wait);
}


static OSKIT_COMDECL 
filesystem_getroot(oskit_filesystem_t * f,
		   struct oskit_dir ** out_dir)
{
	struct sfilesystem *sfs = (struct sfilesystem *) f;
	struct sfiledir *sdir;
	oskit_error_t   rc;


	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	rc = sfiledir_create(sfs, (oskit_file_t *) sfs->root, &sdir);
	if (rc)
		return rc;

	*out_dir = (oskit_dir_t *) & sdir->filei;
	return 0;
}


static OSKIT_COMDECL 
filesystem_remount(oskit_filesystem_t * f,
		   oskit_u32_t flags)
{
	struct sfilesystem *sfs = (struct sfilesystem *) f;
	oskit_security_id_t csid;
	oskit_error_t   rc;


	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FS_HAS_PERM(csid, sfs, REMOUNT);
	if (rc)
		return rc;

	return oskit_filesystem_remount(sfs->fs, flags);
}


static OSKIT_COMDECL 
filesystem_unmount(oskit_filesystem_t * f)
{
	struct sfilesystem *sfs = (struct sfilesystem *) f;
	oskit_security_id_t csid;
	oskit_error_t   rc;


	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FS_HAS_PERM(csid, sfs, UNMOUNT);
	if (rc)
		return rc;

	return oskit_filesystem_unmount(sfs->fs);
}


static OSKIT_COMDECL 
filesystem_lookupi(oskit_filesystem_t * f,
		   oskit_ino_t ino,
		   oskit_file_t ** out_file)
{
	struct sfilesystem *sfs = (struct sfilesystem *) f;
	oskit_security_id_t csid;
	oskit_error_t   rc;


	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FS_HAS_PERM(csid, sfs, LOOKUPI);
	if (rc)
		return rc;

	return oskit_filesystem_lookupi(sfs->fs,ino,out_file);
}


static struct oskit_filesystem_ops fs_ops = {
	filesystem_query,
	filesystem_addref,
	filesystem_release,
	filesystem_statfs,
	filesystem_sync,
	filesystem_getroot,
	filesystem_remount,
	filesystem_unmount,
	filesystem_lookupi,
};



/*
 * oskit_comsid methods
 */

static OSKIT_COMDECL 
sid_query(oskit_comsid_t * s,
	  const struct oskit_guid * iid,
	  void **out_ihandle)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sidi));

	return oskit_filesystem_query(&sfs->fsi, iid, out_ihandle);
}


static OSKIT_COMDECL_U 
sid_addref(oskit_comsid_t * s)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sidi));

	return oskit_filesystem_addref(&sfs->fsi);
}


static OSKIT_COMDECL_U 
sid_release(oskit_comsid_t * s)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sidi));

	return oskit_filesystem_release(&sfs->fsi);
}


static OSKIT_COMDECL 
sid_get(oskit_comsid_t * s,
	oskit_security_class_t * outclass,
	oskit_security_id_t * outsid)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sidi));

	*outclass = OSKIT_SECCLASS_FILESYSTEM;
	*outsid = sfs->sid;
	return 0;
}


static OSKIT_COMDECL 
sid_set(oskit_comsid_t * s,
	oskit_security_id_t newsid)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sidi));

	return oskit_filepsid_setfs(sfs->psid, newsid, OSKIT_SECSID_NULL);
}


static struct oskit_comsid_ops sid_ops = {
	sid_query,
	sid_addref,
	sid_release,
	sid_get,
	sid_set
};



/*
 * oskit_filesystem_secure methods
 */

static OSKIT_COMDECL 
sfs_query(oskit_filesystem_secure_t * s,
	  const struct oskit_guid * iid,
	  void **out_ihandle)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sfsi));

	return oskit_filesystem_query(&sfs->fsi, iid, out_ihandle);
}


static OSKIT_COMDECL_U 
sfs_addref(oskit_filesystem_secure_t * s)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sfsi));

	return oskit_filesystem_addref(&sfs->fsi);
}


static OSKIT_COMDECL_U 
sfs_release(oskit_filesystem_secure_t * s)
{
	struct sfilesystem *sfs;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sfsi));

	return oskit_filesystem_release(&sfs->fsi);
}


static OSKIT_COMDECL 
sfs_chsidfs(oskit_filesystem_secure_t * s,
	    oskit_security_id_t fs_sid,
	    oskit_security_id_t f_sid)
{
	struct sfilesystem *sfs;
	oskit_security_id_t csid;
	oskit_error_t   rc;

	sfs = (struct sfilesystem *) ((char *) s -
				      offsetof(struct sfilesystem, sfsi));

	if (!sfs || !sfs->count || !sfs->fs)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FS_HAS_PERM(csid, sfs, RELABELFROM);
	if (rc)
		return rc;
	rc = oskit_avc_has_perm(sfs->avc, sfs->sid, fs_sid, 
				OSKIT_SECCLASS_FILESYSTEM,
				OSKIT_PERM_FILESYSTEM__TRANSITION);
	if (rc)
		return rc;
	rc = oskit_avc_has_perm(sfs->avc, csid, fs_sid, 
				OSKIT_SECCLASS_FILESYSTEM, 
				OSKIT_PERM_FILESYSTEM__RELABELTO);
	if (rc)
		return rc;
	rc = oskit_avc_has_perm(sfs->avc, f_sid, fs_sid, 
				OSKIT_SECCLASS_FILESYSTEM, 
				OSKIT_PERM_FILESYSTEM__ASSOCIATE);
	if (rc)
		return rc;

	rc = oskit_filepsid_setfs(sfs->psid, fs_sid, f_sid);
	if (rc)
		return rc;
	
	sfs->sid = fs_sid;
	return 0;
}


static struct oskit_filesystem_secure_ops sfs_ops = {
	sfs_query,
	sfs_addref,
	sfs_release,
	sfs_chsidfs
};