chiark / gitweb /
Prep v224: Cleaned up src/basic/util.[ch]
[elogind.git] / src / shared / seccomp-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <seccomp.h>
23
24 #include "util.h"
25 #include "seccomp-util.h"
26
27 const char* seccomp_arch_to_string(uint32_t c) {
28
29         if (c == SCMP_ARCH_NATIVE)
30                 return "native";
31         if (c == SCMP_ARCH_X86)
32                 return "x86";
33         if (c == SCMP_ARCH_X86_64)
34                 return "x86-64";
35         if (c == SCMP_ARCH_X32)
36                 return "x32";
37         if (c == SCMP_ARCH_ARM)
38                 return "arm";
39
40         return NULL;
41 }
42
43 /// UNNEEDED by elogind
44 #if 0
45 int seccomp_arch_from_string(const char *n, uint32_t *ret) {
46         if (!n)
47                 return -EINVAL;
48
49         assert(ret);
50
51         if (streq(n, "native"))
52                 *ret = SCMP_ARCH_NATIVE;
53         else if (streq(n, "x86"))
54                 *ret = SCMP_ARCH_X86;
55         else if (streq(n, "x86-64"))
56                 *ret = SCMP_ARCH_X86_64;
57         else if (streq(n, "x32"))
58                 *ret = SCMP_ARCH_X32;
59         else if (streq(n, "arm"))
60                 *ret = SCMP_ARCH_ARM;
61         else
62                 return -EINVAL;
63
64         return 0;
65 }
66 #endif // 0
67
68 int seccomp_add_secondary_archs(scmp_filter_ctx *c) {
69
70 #if defined(__i386__) || defined(__x86_64__)
71         int r;
72
73         /* Add in all possible secondary archs we are aware of that
74          * this kernel might support. */
75
76         r = seccomp_arch_add(c, SCMP_ARCH_X86);
77         if (r < 0 && r != -EEXIST)
78                 return r;
79
80         r = seccomp_arch_add(c, SCMP_ARCH_X86_64);
81         if (r < 0 && r != -EEXIST)
82                 return r;
83
84         r = seccomp_arch_add(c, SCMP_ARCH_X32);
85         if (r < 0 && r != -EEXIST)
86                 return r;
87
88 #endif
89
90         return 0;
91
92 }