~ressu/+junk/xen-debian

« back to all changes in this revision

Viewing changes to tools/libxc/xc_flask.c

  • Committer: sami at haahtinen
  • Author(s): Bastian Blank
  • Date: 2011-03-17 14:12:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: sami@haahtinen.name-20110317141245-owgqox0l0p3g5857
Tags: 4.1.0~rc6-1
* New upstream release candidate.
* Build documentation using pdflatex.
* Use python 2.6. (closes: #596545)
* Fix lintian override.
* Install new tools: xl, xenpaging.
* Enable blktap2.
  - Use own md5 implementation.
  - Fix includes.
  - Fix linking of blktap2 binaries.
  - Remove optimization setting.
* Temporarily disable hvmloader, wants to download ipxe.
* Remove xenstored pid check from xl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
2
 * xc_flask.c
3
3
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License as
6
 
 * published by the Free Software Foundation, version 2 of the
7
 
 * License.
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation;
 
7
 * version 2.1 of the License.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
8
17
 */
9
18
 
10
19
#include "xc_private.h"
11
 
 
12
 
int xc_flask_op(int xc_handle, flask_op_t *op)
 
20
#include <unistd.h>
 
21
#include <stdio.h>
 
22
#include <errno.h>
 
23
#include <fcntl.h>
 
24
#include <string.h>
 
25
#include <sys/mman.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <stdlib.h>
 
29
#include <stdint.h>
 
30
#include <sys/ioctl.h>
 
31
#include <stdint.h>
 
32
 
 
33
#define OCON_PIRQ_STR   "pirq"
 
34
#define OCON_IOPORT_STR "ioport"
 
35
#define OCON_IOMEM_STR  "iomem"
 
36
#define OCON_DEVICE_STR "pcidevice"
 
37
#define INITCONTEXTLEN  256
 
38
 
 
39
int xc_flask_op(xc_interface *xch, flask_op_t *op)
13
40
{
14
41
    int ret = -1;
15
42
    DECLARE_HYPERCALL;
 
43
    DECLARE_HYPERCALL_BOUNCE(op, sizeof(*op), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
44
 
 
45
    if ( xc_hypercall_bounce_pre(xch, op) )
 
46
    {
 
47
        PERROR("Could not bounce memory for flask op hypercall");
 
48
        goto out;
 
49
    }
16
50
 
17
51
    hypercall.op     = __HYPERVISOR_xsm_op;
18
 
    hypercall.arg[0] = (unsigned long)op;
19
 
 
20
 
    if ( mlock(op, sizeof(*op)) != 0 )
21
 
    {
22
 
        PERROR("Could not lock memory for Xen hypercall");
23
 
        goto out;
24
 
    }
25
 
 
26
 
    if ( (ret = do_xen_hypercall(xc_handle, &hypercall)) < 0 )
 
52
    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(op);
 
53
 
 
54
    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
27
55
    {
28
56
        if ( errno == EACCES )
29
57
            fprintf(stderr, "XSM operation failed!\n");
30
58
    }
31
59
 
32
 
    safe_munlock(op, sizeof(*op));
 
60
    xc_hypercall_bounce_post(xch, op);
33
61
 
34
62
 out:
35
63
    return ret;
36
64
}
37
65
 
 
66
int xc_flask_load(xc_interface *xc_handle, char *buf, uint32_t size)
 
67
{
 
68
    int err;
 
69
    flask_op_t op;
 
70
    
 
71
    op.cmd = FLASK_LOAD;
 
72
    op.buf = buf;
 
73
    op.size = size;
 
74
    
 
75
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
76
        return err;
 
77
 
 
78
    return 0;
 
79
}
 
80
 
 
81
int xc_flask_context_to_sid(xc_interface *xc_handle, char *buf, uint32_t size, uint32_t *sid)
 
82
{
 
83
    int err;
 
84
    flask_op_t op;
 
85
    
 
86
    op.cmd = FLASK_CONTEXT_TO_SID;
 
87
    op.buf = buf;
 
88
    op.size = size;
 
89
    
 
90
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
91
        return err;
 
92
    
 
93
    sscanf(buf, "%u", sid);
 
94
 
 
95
    return 0;
 
96
}
 
97
 
 
98
int xc_flask_sid_to_context(xc_interface *xc_handle, int sid, char *buf, uint32_t size)
 
99
{
 
100
    int err;
 
101
    flask_op_t op;
 
102
    
 
103
    op.cmd = FLASK_SID_TO_CONTEXT;
 
104
    op.buf = buf;
 
105
    op.size = size;
 
106
    
 
107
    snprintf(buf, size, "%u", sid);
 
108
 
 
109
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
110
        return err;
 
111
 
 
112
    return 0;
 
113
}
 
114
 
 
115
int xc_flask_getenforce(xc_interface *xc_handle)
 
116
{
 
117
    int err;
 
118
    flask_op_t op;
 
119
    char buf[20];            
 
120
    int size = 20;
 
121
    int mode;
 
122
 
 
123
    op.cmd = FLASK_GETENFORCE;
 
124
    op.buf = buf;
 
125
    op.size = size;
 
126
    
 
127
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
128
        return err;
 
129
 
 
130
    sscanf(buf, "%i", &mode);
 
131
 
 
132
    return mode;
 
133
}
 
134
 
 
135
int xc_flask_setenforce(xc_interface *xc_handle, int mode)
 
136
{
 
137
    int err;
 
138
    flask_op_t op;
 
139
    char buf[20];
 
140
    int size = 20; 
 
141
 
 
142
    op.cmd = FLASK_SETENFORCE;
 
143
    op.buf = buf;
 
144
    op.size = size;
 
145
   
 
146
    snprintf(buf, size, "%i", mode);
 
147
 
 
148
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
149
        return err;
 
150
 
 
151
    return 0;
 
152
}
 
153
 
 
154
static int xc_flask_add(xc_interface *xc_handle, char *cat, char *arg, char *scontext)
 
155
{
 
156
    char buf[512];
 
157
    flask_op_t op;
 
158
 
 
159
    memset(buf, 0, 512);
 
160
    snprintf(buf, 512, "%s %255s %s", cat, scontext, arg);
 
161
    op.cmd = FLASK_ADD_OCONTEXT;
 
162
    op.buf = buf;
 
163
    op.size = 512;
 
164
    
 
165
    return xc_flask_op(xc_handle, &op);
 
166
}
 
167
 
 
168
int xc_flask_add_pirq(xc_interface *xc_handle, unsigned int pirq, char *scontext)
 
169
{
 
170
    char arg[16];
 
171
 
 
172
    snprintf(arg, 16, "%u", pirq);
 
173
    return xc_flask_add(xc_handle, OCON_PIRQ_STR, arg, scontext);
 
174
}
 
175
 
 
176
int xc_flask_add_ioport(xc_interface *xc_handle, unsigned long low, unsigned long high,
 
177
                      char *scontext)
 
178
{
 
179
    char arg[64];
 
180
 
 
181
    snprintf(arg, 64, "%lu %lu", low, high);
 
182
    return xc_flask_add(xc_handle, OCON_IOPORT_STR, arg, scontext);
 
183
}
 
184
 
 
185
int xc_flask_add_iomem(xc_interface *xc_handle, unsigned long low, unsigned long high,
 
186
                     char *scontext)
 
187
{
 
188
    char arg[64];
 
189
 
 
190
    snprintf(arg, 64, "%lu %lu", low, high);
 
191
    return xc_flask_add(xc_handle, OCON_IOMEM_STR, arg, scontext);
 
192
}
 
193
 
 
194
int xc_flask_add_device(xc_interface *xc_handle, unsigned long device, char *scontext)
 
195
{
 
196
    char arg[32];
 
197
 
 
198
    snprintf(arg, 32, "%lu", device);
 
199
    return xc_flask_add(xc_handle, OCON_DEVICE_STR, arg, scontext);
 
200
}
 
201
 
 
202
static int xc_flask_del(xc_interface *xc_handle, char *cat, char *arg)
 
203
{
 
204
    char buf[256];
 
205
    flask_op_t op;
 
206
 
 
207
    memset(buf, 0, 256);
 
208
    snprintf(buf, 256, "%s %s", cat, arg);
 
209
    op.cmd = FLASK_DEL_OCONTEXT;
 
210
    op.buf = buf;
 
211
    op.size = 256;
 
212
    
 
213
    return xc_flask_op(xc_handle, &op);
 
214
}
 
215
 
 
216
int xc_flask_del_pirq(xc_interface *xc_handle, unsigned int pirq)
 
217
{
 
218
    char arg[16];
 
219
 
 
220
    snprintf(arg, 16, "%u", pirq);
 
221
    return xc_flask_del(xc_handle, OCON_PIRQ_STR, arg);
 
222
}
 
223
 
 
224
int xc_flask_del_ioport(xc_interface *xc_handle, unsigned long low, unsigned long high)
 
225
{
 
226
    char arg[64];
 
227
 
 
228
    snprintf(arg, 64, "%lu %lu", low, high);
 
229
    return xc_flask_del(xc_handle, OCON_IOPORT_STR, arg);
 
230
}
 
231
 
 
232
int xc_flask_del_iomem(xc_interface *xc_handle, unsigned long low, unsigned long high)
 
233
{
 
234
    char arg[64];
 
235
 
 
236
    snprintf(arg, 64, "%lu %lu", low, high);
 
237
    return xc_flask_del(xc_handle, OCON_IOMEM_STR, arg);
 
238
}
 
239
 
 
240
int xc_flask_del_device(xc_interface *xc_handle, unsigned long device)
 
241
{
 
242
    char arg[32];
 
243
 
 
244
    snprintf(arg, 32, "%lu", device);
 
245
    return xc_flask_del(xc_handle, OCON_DEVICE_STR, arg);
 
246
}
 
247
 
 
248
int xc_flask_access(xc_interface *xc_handle, const char *scon, const char *tcon,
 
249
                uint16_t tclass, uint32_t req,
 
250
                uint32_t *allowed, uint32_t *decided,
 
251
                uint32_t *auditallow, uint32_t *auditdeny,
 
252
                uint32_t *seqno)
 
253
{
 
254
/* maximum number of digits in a 16-bit decimal number: */
 
255
#define MAX_SHORT_DEC_LEN 5
 
256
 
 
257
    char *buf;
 
258
    int bufLen;
 
259
    int err;
 
260
    flask_op_t op;
 
261
    uint32_t dummy_allowed;
 
262
    uint32_t dummy_decided;
 
263
    uint32_t dummy_auditallow;
 
264
    uint32_t dummy_auditdeny;
 
265
    uint32_t dummy_seqno;
 
266
  
 
267
    if (!allowed)
 
268
        allowed = &dummy_allowed;
 
269
    if (!decided)
 
270
        decided = &dummy_decided;
 
271
    if (!auditallow)
 
272
        auditallow = &dummy_auditallow;
 
273
    if (!auditdeny)
 
274
        auditdeny = &dummy_auditdeny;
 
275
    if (!seqno)
 
276
        seqno = &dummy_seqno;
 
277
 
 
278
    if (!scon)
 
279
        return -EINVAL;
 
280
    if (!tcon)
 
281
        return -EINVAL;
 
282
 
 
283
    bufLen = strlen(scon) + 1 + strlen(tcon) + 1 +
 
284
        MAX_SHORT_DEC_LEN + 1 +
 
285
        sizeof(req)*2 + 1;
 
286
    buf = malloc(bufLen);
 
287
    snprintf(buf, bufLen, "%s %s %hu %x", scon, tcon, tclass, req);
 
288
 
 
289
    op.cmd = FLASK_ACCESS;
 
290
    op.buf = buf;
 
291
    op.size = strlen(buf)+1;
 
292
    
 
293
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
294
    {
 
295
        free(buf);
 
296
        return err;
 
297
    }
 
298
   
 
299
    if (sscanf(op.buf, "%x %x %x %x %u",
 
300
               allowed, decided,
 
301
               auditallow, auditdeny,
 
302
               seqno) != 5) {
 
303
        err = -EILSEQ;
 
304
    }
 
305
 
 
306
    err = ((*allowed & req) == req)? 0 : -EPERM;
 
307
 
 
308
    return err;
 
309
 
 
310
}
 
311
 
 
312
int xc_flask_avc_hashstats(xc_interface *xc_handle, char *buf, int size)
 
313
{
 
314
    int err;
 
315
    flask_op_t op;
 
316
  
 
317
    op.cmd = FLASK_AVC_HASHSTATS;
 
318
    op.buf = buf;
 
319
    op.size = size;
 
320
  
 
321
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
322
    {
 
323
        free(buf);
 
324
        return err;
 
325
    }
 
326
 
 
327
    return 0;
 
328
}
 
329
 
 
330
int xc_flask_avc_cachestats(xc_interface *xc_handle, char *buf, int size)
 
331
{
 
332
    int err;
 
333
    flask_op_t op;
 
334
  
 
335
    op.cmd = FLASK_AVC_CACHESTATS;
 
336
    op.buf = buf;
 
337
    op.size = size;
 
338
  
 
339
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
340
    {
 
341
        free(buf);
 
342
        return err;
 
343
    }
 
344
 
 
345
    return 0;
 
346
}
 
347
 
 
348
int xc_flask_policyvers(xc_interface *xc_handle, char *buf, int size)
 
349
{
 
350
    int err;
 
351
    flask_op_t op;
 
352
  
 
353
    op.cmd = FLASK_POLICYVERS;
 
354
    op.buf = buf;
 
355
    op.size = size;
 
356
 
 
357
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
358
    {
 
359
        free(buf);
 
360
        return err;
 
361
    }
 
362
 
 
363
    return 0;
 
364
}
 
365
 
 
366
int xc_flask_getavc_threshold(xc_interface *xc_handle)
 
367
{
 
368
    int err;
 
369
    flask_op_t op;
 
370
    char buf[20];            
 
371
    int size = 20;
 
372
    int threshold;
 
373
 
 
374
    op.cmd = FLASK_GETAVC_THRESHOLD;
 
375
    op.buf = buf;
 
376
    op.size = size;
 
377
    
 
378
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
379
        return err;
 
380
 
 
381
    sscanf(buf, "%i", &threshold);
 
382
 
 
383
    return threshold;
 
384
}
 
385
 
 
386
int xc_flask_setavc_threshold(xc_interface *xc_handle, int threshold)
 
387
{
 
388
    int err;
 
389
    flask_op_t op;
 
390
    char buf[20];            
 
391
    int size = 20;
 
392
 
 
393
    op.cmd = FLASK_SETAVC_THRESHOLD;
 
394
    op.buf = buf;
 
395
    op.size = size;
 
396
 
 
397
    snprintf(buf, size, "%i", threshold);
 
398
 
 
399
    if ( (err = xc_flask_op(xc_handle, &op)) != 0 )
 
400
        return err;
 
401
 
 
402
    return 0;
 
403
}
 
404
 
38
405
/*
39
406
 * Local variables:
40
407
 * mode: C