File: tcap.c

package info (click to toggle)
diod 1.0.24-5.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 29,200 kB
  • sloc: ansic: 33,159; sh: 6,545; makefile: 363; perl: 80
file content (197 lines) | stat: -rw-r--r-- 4,940 bytes parent folder | download | duplicates (7)
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
/* tcap.c - check that pthreads can independently set capabilities */

#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/fsuid.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#if HAVE_LIBCAP
#include <sys/capability.h>
#endif
#include <grp.h>

#include "diod_log.h"

#include "test.h"

#if HAVE_LIBCAP
typedef enum { S0, S1, S2, S3, S4, S5 } state_t;

static state_t         state = S0;
static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  state_cond = PTHREAD_COND_INITIALIZER;

static void
_prtcap (char *s, cap_value_t capflag)
{
    cap_t cap;
    cap_flag_value_t val;

    if (!(cap = cap_get_proc ()))
        err_exit ("%s: cap_get_proc", s);
    if (cap_get_flag (cap, capflag, CAP_EFFECTIVE, &val) < 0)
        err_exit ("%s: cap_get_flag", s);
    if (cap_free (cap) < 0)
        err_exit ("%s: cap_free", s);
    msg ("%s: cap is %s", s, val == CAP_SET ? "set" : "clear");
}

static void
_setcap (char *s, cap_value_t capflag)
{
    cap_t cap;

    if (!(cap = cap_get_proc ()))
        err_exit ("%s: cap_get_proc", s);
    if (cap_set_flag (cap, CAP_EFFECTIVE, 1, &capflag, CAP_SET) < 0)
        err_exit ("%s: cap_set_flag", s); 
    if (cap_set_proc (cap) < 0)
        err_exit ("%s: cap_set_proc", s); 
    if (cap_free (cap) < 0)
        err_exit ("%s: cap_free", s);
}

static void
_clrcap (char *s, cap_value_t capflag)
{
    cap_t cap;

    if (!(cap = cap_get_proc ()))
        err_exit ("%s: cap_get_proc", s);
    if (cap_set_flag (cap, CAP_EFFECTIVE, 1, &capflag, CAP_CLEAR) < 0)
        err_exit ("%s: cap_set_flag", s); 
    if (cap_set_proc (cap) < 0)
        err_exit ("%s: cap_set_proc", s); 
    if (cap_free (cap) < 0)
        err_exit ("%s: cap_free", s);
}

static void
change_state (state_t s)
{
    _lock (&state_lock);
    state = s;
    _condsig (&state_cond);
    _unlock (&state_lock);
}

static void
wait_state (state_t s)
{
    _lock (&state_lock);
    while ((state != s))
        _condwait (&state_cond, &state_lock);
    _unlock (&state_lock);
}

static void *proc1 (void *a)
{
    _prtcap ("task1", CAP_DAC_OVERRIDE); /* 1) task 1, expect clr */
    _prtcap ("task1", CAP_CHOWN);
    change_state (S1);

    wait_state (S2);
    _prtcap ("task1", CAP_DAC_OVERRIDE); /* 4) task 1, still clr */
    _prtcap ("task1", CAP_CHOWN);

    msg ("task1: clr cap");
    _clrcap ("task1", CAP_DAC_OVERRIDE);
    change_state (S3);

    wait_state (S4);
    return NULL;
}

static void *proc2 (void *a)
{
    wait_state (S1);
    _prtcap ("task2", CAP_DAC_OVERRIDE); /* 2) task 2, expect clr */
    _prtcap ("task2", CAP_CHOWN);

    msg ("task2: set cap");
    _setcap ("task2", CAP_DAC_OVERRIDE);
    _setcap ("task2", CAP_CHOWN);
    _prtcap ("task2", CAP_DAC_OVERRIDE); /* 3) task 2, expect set */
    _prtcap ("task2", CAP_CHOWN);
    change_state (S2);

    wait_state (S3);
    _prtcap ("task2", CAP_DAC_OVERRIDE); /* 5) task 2, expect set */
    _prtcap ("task2", CAP_CHOWN);
    change_state (S4);
    return NULL;
}
#endif

int main(int argc, char *argv[])
{
#if HAVE_LIBCAP
    pthread_t t1, t2;

    assert (geteuid () == 0);

    diod_log_init (argv[0]);
    _prtcap ("task0", CAP_DAC_OVERRIDE); /* root, expect set */
    _prtcap ("task0", CAP_CHOWN);

    msg ("task0: setfsuid 1");
    setfsuid (1); 
    _prtcap ("task0", CAP_DAC_OVERRIDE); /* non-root, expect clr */
    _prtcap ("task0", CAP_CHOWN);

    msg ("task0: setfsuid 0");          /* root, expect set */
    setfsuid (0); 
    _prtcap ("task0", CAP_DAC_OVERRIDE);
    _prtcap ("task0", CAP_CHOWN);

    msg ("task0: setfsuid 1");
    setfsuid (1); 
    _prtcap ("task0", CAP_DAC_OVERRIDE); /* non-root, expect clr */
    _prtcap ("task0", CAP_CHOWN);

    msg ("task0: set cap");
    _setcap ("task0", CAP_DAC_OVERRIDE);
    _setcap ("task0", CAP_CHOWN);
    _prtcap ("task0", CAP_DAC_OVERRIDE); /* root with cap explicitly set, */
    _prtcap ("task0", CAP_CHOWN);        /*  expect set */

    msg ("task0: setfsuid 2");
    setfsuid (2); 
    _prtcap ("task0", CAP_DAC_OVERRIDE);/* non-root with cap explicitly set, */
    _prtcap ("task0", CAP_CHOWN);       /*  (as root) expect set */

    msg ("task0: clr cap");
    _clrcap ("task0", CAP_DAC_OVERRIDE);
    _clrcap ("task0", CAP_CHOWN);
    _prtcap ("task0", CAP_DAC_OVERRIDE);/* non-root with cap explicitly clr, */
    _prtcap ("task0", CAP_CHOWN);       /* (as non-root) expect clr */

    _create (&t1, proc1, NULL);
    _create (&t2, proc2, NULL);

    _join (t2, NULL);
    _join (t1, NULL);

    _prtcap ("task0", CAP_DAC_OVERRIDE); /* after threads, expect clr */
    _prtcap ("task0", CAP_CHOWN);
#else
    fprintf (stderr, "libcap unavailable\n");
    exit (77);
#endif

    exit (0);
}

/*
 * vi:tabstop=4 shiftwidth=4 expandtab
 */