~ubuntu-branches/ubuntu/quantal/lxc/quantal-201208301614

« back to all changes in this revision

Viewing changes to src/lxc/seccomp.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn, Stéphane Graber, Serge Hallyn
  • Date: 2012-08-08 10:43:06 UTC
  • Revision ID: package-import@ubuntu.com-20120808104306-2s7xdim4rvt0e2k6
Tags: 0.8.0~rc1-4ubuntu22
[ Stéphane Graber ]
* Fix call to echo in lxc-start-ephemeral that was literally showing
  '$LXC_BASE' instead of the variable's value.

[ Serge Hallyn ]
* Introduce support for seccomp.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lxc: linux Container library
 
3
 *
 
4
 * (C) Copyright Canonical, Inc. 2012
 
5
 *
 
6
 * Authors:
 
7
 * Serge Hallyn <serge.hallyn@canonical.com>
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2.1 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
22
 */
 
23
 
 
24
#define _GNU_SOURCE
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <seccomp.h>
 
28
#include <errno.h>
 
29
#include <seccomp.h>
 
30
#include "lxcseccomp.h"
 
31
 
 
32
#include "log.h"
 
33
 
 
34
lxc_log_define(lxc_seccomp, lxc);
 
35
 
 
36
/*
 
37
 * The first line of the config file has a policy language version
 
38
 * the second line has some directives
 
39
 * then comes policy subject to the directives
 
40
 * right now version must be '1'
 
41
 * the directives must include 'whitelist' (only type of policy currently
 
42
 * supported) and can include 'debug' (though debug is not yet supported).
 
43
 */
 
44
static int parse_config(FILE *f, struct lxc_conf *conf)
 
45
{
 
46
        char line[1024];
 
47
        int ret, version;
 
48
 
 
49
        ret = fscanf(f, "%d\n", &version);
 
50
        if (ret != 1 || version != 1) {
 
51
                ERROR("invalid version");
 
52
                return -1;
 
53
        }
 
54
        if (!fgets(line, 1024, f)) {
 
55
                ERROR("invalid config file");
 
56
                return -1;
 
57
        }
 
58
        if (!strstr(line, "whitelist")) {
 
59
                ERROR("only whitelist policy is supported");
 
60
                return -1;
 
61
        }
 
62
        if (strstr(line, "debug")) {
 
63
                ERROR("debug not yet implemented");
 
64
                return -1;
 
65
        }
 
66
        /* now read in the whitelist entries one per line */
 
67
        while (fgets(line, 1024, f)) {
 
68
                int nr;
 
69
                ret = sscanf(line, "%d", &nr);
 
70
                if (ret != 1)
 
71
                        return -1;
 
72
                ret = seccomp_rule_add(SCMP_ACT_ALLOW, nr, 0);
 
73
                if (ret < 0) {
 
74
                        ERROR("failed loading allow rule for %d\n", nr);
 
75
                        return ret;
 
76
                }
 
77
        }
 
78
        return 0;
 
79
}
 
80
 
 
81
int lxc_read_seccomp_config(struct lxc_conf *conf)
 
82
{
 
83
        FILE *f;
 
84
        int ret;
 
85
 
 
86
        if (seccomp_init(SCMP_ACT_ERRNO(31)) < 0)  { /* for debug, pass in SCMP_ACT_TRAP */
 
87
                ERROR("failed initializing seccomp");
 
88
                return -1;
 
89
        }
 
90
        if (!conf->seccomp)
 
91
                return 0;
 
92
 
 
93
        /* turn of no-new-privs.  We don't want it in lxc, and it breaks
 
94
         * with apparmor */
 
95
        if (seccomp_attr_set(SCMP_FLTATR_CTL_NNP, 0)) {
 
96
                ERROR("failed to turn off n-new-privs\n");
 
97
                return -1;
 
98
        }
 
99
 
 
100
        f = fopen(conf->seccomp, "r");
 
101
        if (!f) {
 
102
                SYSERROR("failed to open seccomp policy file %s\n", conf->seccomp);
 
103
                return -1;
 
104
        }
 
105
        ret = parse_config(f, conf);
 
106
        fclose(f);
 
107
        return ret;
 
108
}
 
109
 
 
110
int lxc_seccomp_load(struct lxc_conf *conf)
 
111
{
 
112
        int ret;
 
113
        if (!conf->seccomp)
 
114
                return 0;
 
115
        ret = seccomp_load();
 
116
        if (ret < 0) {
 
117
                ERROR("Error loading the seccomp policy");
 
118
                return -1;
 
119
        }
 
120
        return 0;
 
121
}