Whamcloud - gitweb
current branches now use lnet from HEAD
[fs/lustre-release.git] / lustre / obdfilter / filter_capa.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004, 2005 Cluster File Systems, Inc.
5  *
6  * Author: Lai Siyao <lsy@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre 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
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_FILTER
25
26 #include <linux/fs.h>
27 #include <linux/version.h>
28 #include <asm/uaccess.h>
29 #include <linux/file.h>
30 #include <linux/kmod.h>
31
32 #include <linux/lustre_fsfilt.h>
33 #include <linux/lustre_sec.h>
34
35 #include "filter_internal.h"
36
37 /*
38  * FIXME
39  * keep this as simple as possible. we suppose the blacklist usually
40  * be empry or very short (<5), since long term blacklist should be
41  * done on MDS side. A more sophisticated blacklist will be implemented
42  * later.
43  *
44  * note blacklist didn't take effect when OSS capability disabled. this
45  * looks reasonable to me.
46  */
47 #define BLACKLIST_MAX   (32)
48 static int nblacklist = 0;
49 static uid_t blacklist[BLACKLIST_MAX];
50 static spinlock_t blacklist_lock = SPIN_LOCK_UNLOCKED;
51
52 int blacklist_display(char *buf, int bufsize)
53 {
54         char one[16];
55         int i;
56         LASSERT(buf);
57
58         buf[0] = '\0';
59         spin_lock(&blacklist_lock);
60         for (i = 0; i < nblacklist; i++) {
61                 snprintf(one, 16, "%u\n", blacklist[i]);
62                 strncat(buf, one, bufsize);
63         }
64         spin_unlock(&blacklist_lock);
65         return strnlen(buf, bufsize);
66 }
67
68 void blacklist_add(uid_t uid)
69 {
70         int i;
71
72         spin_lock(&blacklist_lock);
73         if (nblacklist == BLACKLIST_MAX) {
74                 CERROR("can't add more in blacklist\n");
75                 spin_unlock(&blacklist_lock);
76                 return;
77         }
78
79         for (i = 0; i < nblacklist; i++) {
80                 if (blacklist[i] == uid) {
81                         spin_unlock(&blacklist_lock);
82                         return;
83                 }
84         }
85
86         blacklist[nblacklist++] = uid;
87         spin_unlock(&blacklist_lock);
88 }
89
90 void blacklist_del(uid_t uid)
91 {
92         int i;
93
94         spin_lock(&blacklist_lock);
95         for (i = 0; i < nblacklist; i++) {
96                 if (blacklist[i] == uid) {
97                         nblacklist--;
98                         while (i < nblacklist) {
99                                 blacklist[i] = blacklist[i+1];
100                                 i++;
101                         }
102                         spin_unlock(&blacklist_lock);
103                         return;
104                 }
105         }
106         spin_unlock(&blacklist_lock);
107 }
108
109 int blacklist_check(uid_t uid)
110 {
111         int i, rc = 0;
112
113         spin_lock(&blacklist_lock);
114         for (i = 0; i < nblacklist; i++) {
115                 if (blacklist[i] == uid) {
116                         rc = 1;
117                         break;
118                 }
119         }
120         spin_unlock(&blacklist_lock);
121         return rc;
122 }
123
124
125 void filter_free_capa_keys(struct filter_obd *filter)
126 {
127         struct filter_capa_key *key, *n;
128
129         spin_lock(&filter->fo_capa_lock);
130         list_for_each_entry_safe(key, n, &filter->fo_capa_keys, k_list) {
131                 list_del_init(&key->k_list);
132                 OBD_FREE(key, sizeof(*key));
133         }
134         spin_unlock(&filter->fo_capa_lock);
135 }
136
137 int filter_update_capa_key(struct obd_device *obd, struct lustre_capa_key *key)
138 {
139         struct filter_obd *filter = &obd->u.filter;
140         struct filter_capa_key *tmp = NULL, *rkey = NULL, *bkey = NULL;
141         int rc = 0;
142         ENTRY;
143
144         spin_lock(&filter->fo_capa_lock);
145         list_for_each_entry(tmp, &filter->fo_capa_keys, k_list) {
146                 if (tmp->k_key.lk_mdsid != le32_to_cpu(key->lk_mdsid))
147                         continue;
148
149                 if (rkey == NULL)
150                         rkey = tmp;
151                 else
152                         bkey = tmp;
153         }
154         spin_unlock(&filter->fo_capa_lock);
155
156         if (rkey && bkey && capa_key_cmp(&rkey->k_key, &bkey->k_key) < 0) {
157                 tmp = rkey;
158                 rkey = bkey;
159                 bkey = tmp;
160         }
161
162         if (bkey) {
163                 tmp = bkey;
164
165                 DEBUG_CAPA_KEY(D_INFO, &tmp->k_key, "filter update");
166         } else {
167                 OBD_ALLOC(tmp, sizeof(*tmp));
168                 if (!tmp)
169                         GOTO(out, rc = -ENOMEM);
170
171                 DEBUG_CAPA_KEY(D_INFO, &tmp->k_key, "filter new");
172         }
173
174         /* fields in lustre_capa_key are in cpu order */
175         spin_lock(&filter->fo_capa_lock);
176         tmp->k_key.lk_mdsid = le32_to_cpu(key->lk_mdsid);
177         tmp->k_key.lk_keyid = le32_to_cpu(key->lk_keyid);
178         tmp->k_key.lk_expiry = le64_to_cpu(key->lk_expiry);
179         memcpy(&tmp->k_key.lk_key, key->lk_key, sizeof(key->lk_key));
180
181         if (!bkey)
182                 list_add_tail(&tmp->k_list, &filter->fo_capa_keys);
183         spin_unlock(&filter->fo_capa_lock);
184 out:
185         RETURN(rc);
186 }
187
188 int filter_verify_fid(struct obd_export *exp, struct inode *inode,
189                       struct lustre_capa *capa)
190 {
191         struct lustre_id fid;
192         int rc;
193
194         if (!capa)
195                 return 0;
196
197         ENTRY;
198         rc = fsfilt_get_md(exp->exp_obd, inode, &fid, sizeof(fid), EA_SID);
199         if (rc < 0) {
200                 CERROR("get fid from object failed! rc:%d\n", rc);
201                 RETURN(rc);
202         } else if (rc > 0) {
203                 if (capa->lc_mdsid != id_group(&fid) ||
204                     capa->lc_ino != id_ino(&fid))
205                         RETURN(-EINVAL);
206         }
207
208         RETURN(0);
209 }
210
211 int
212 filter_verify_capa(int cmd, struct obd_export *exp, struct lustre_capa *capa)
213 {
214         struct obd_device *obd = exp->exp_obd;
215         struct filter_obd *filter = &obd->u.filter;
216         struct obd_capa *ocapa;
217         struct lustre_capa tcapa;
218         struct filter_capa_key *rkey = NULL, *bkey = NULL, *tmp, capa_keys[2];
219         int rc = 0;
220
221         /* capability is disabled */
222         if (filter->fo_capa_stat == 0)
223                 RETURN(0);
224
225         ENTRY;
226         if (capa == NULL) {
227                 CDEBUG(D_ERROR, "no capa has been passed\n");
228                 RETURN(-EACCES);
229         }
230
231         if (blacklist_check(capa->lc_uid)) {
232                 DEBUG_CAPA(D_ERROR, capa, "found in blacklist\n");
233                 RETURN(-EACCES);
234         }
235
236         if (cmd == OBD_BRW_WRITE && !(capa->lc_op & (CAPA_WRITE | CAPA_TRUNC))) {
237                 DEBUG_CAPA(D_ERROR, capa, "have no write access\n");
238                 RETURN(-EACCES);
239         }
240         if (cmd == OBD_BRW_READ && !(capa->lc_op & (CAPA_WRITE | CAPA_READ))) {
241                 DEBUG_CAPA(D_ERROR, capa, "have no read access\n");
242                 RETURN(-EACCES);
243         }
244
245         if (OBD_FAIL_CHECK(OBD_FAIL_FILTER_VERIFY_CAPA))
246                 RETURN(-EACCES);
247
248         if (capa_expired(capa)) {
249                 DEBUG_CAPA(D_INFO | D_ERROR, capa, "expired");
250                 RETURN(-ESTALE);
251         }
252
253         ocapa = filter_capa_get(capa);
254 verify:
255         if (ocapa) {
256                 struct timeval tv;
257
258                 /* fo_capa_lock protects capa too */
259                 do_gettimeofday(&tv);
260                 spin_lock(&filter->fo_capa_lock);
261                 if (capa->lc_keyid == ocapa->c_capa.lc_keyid) {
262                         rc = memcmp(capa, &ocapa->c_capa, sizeof(*capa));
263                 } else if (ocapa->c_bvalid &&
264                            capa->lc_keyid == ocapa->c_bkeyid) {
265                         rc = memcmp(capa->lc_hmac, ocapa->c_bhmac,
266                                     sizeof(capa->lc_hmac));
267                 } else {
268                         /* ocapa is obsolete too */
269                         ocapa->c_bvalid = 0;
270                         goto new_capa;
271                 }
272
273                 if (rc && __capa_is_to_expire(ocapa, &tv)) {
274                         /* client should use new expiry now */
275                         ocapa->c_bvalid = 0;
276                         goto new_capa;
277                 }
278                 spin_unlock(&filter->fo_capa_lock);
279
280                 if (rc) {
281                         char *key1 = NULL, *key2 = NULL;
282                         OBD_ALLOC(key1, CAPA_DIGEST_SIZE * 2 + 1);
283                         OBD_ALLOC(key2, CAPA_DIGEST_SIZE * 2 + 1);
284                         if (key1 && key2) {
285                                 dump_capa_hmac(key1, capa->lc_hmac);
286                                 dump_capa_hmac(key2, ocapa->c_capa.lc_hmac);
287                                 DEBUG_CAPA(D_ERROR, capa,
288                                            "access denied for (%s != %s)",
289                                            key1, key2);
290                                 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "used capa");
291                         }
292                         if (key1)
293                                 OBD_FREE(key1, CAPA_DIGEST_SIZE * 2 + 1);
294                         if (key2)
295                                 OBD_FREE(key2, CAPA_DIGEST_SIZE * 2 + 1);
296                 }
297                 capa_put(ocapa);
298                 RETURN(rc ? -EACCES : 0);
299         }
300
301         spin_lock(&filter->fo_capa_lock);
302 new_capa:
303         list_for_each_entry(tmp, &filter->fo_capa_keys, k_list) {
304                 if (tmp->k_key.lk_mdsid == capa->lc_mdsid) {
305                         if (rkey == NULL)
306                                 rkey = tmp;
307                         else
308                                 bkey = tmp;
309                 }
310         }
311
312         if (rkey && bkey && capa_key_cmp(&rkey->k_key, &bkey->k_key) < 0) {
313                 tmp = rkey;
314                 rkey = bkey;
315                 bkey = tmp;
316         }
317
318         if ((!rkey || rkey->k_key.lk_keyid != capa->lc_keyid) &&
319             (!bkey || bkey->k_key.lk_keyid != capa->lc_keyid)) {
320                 spin_unlock(&filter->fo_capa_lock);
321                 GOTO(out, rc = -ESTALE);
322         }
323
324         LASSERT(rkey);
325         capa_keys[0] = *rkey;
326         if (bkey)
327                 capa_keys[1] = *bkey;
328         spin_unlock(&filter->fo_capa_lock);
329
330         tcapa = *capa;
331         tcapa.lc_keyid = capa_keys[0].k_key.lk_keyid;
332         capa_hmac(capa_keys[0].k_key.lk_key, &tcapa);
333
334         /* store in capa cache */
335         ocapa = capa_renew(&tcapa, FILTER_CAPA);
336         if (!ocapa)
337                 GOTO(out, rc = -ENOMEM);
338
339         if (bkey) {
340                 tcapa.lc_keyid = capa_keys[1].k_key.lk_keyid;
341                 capa_hmac(capa_keys[1].k_key.lk_key, &tcapa);
342
343                 spin_lock(&filter->fo_capa_lock);
344                 memcpy(ocapa->c_bhmac, tcapa.lc_hmac, sizeof(ocapa->c_bhmac));
345                 ocapa->c_bkeyid = capa_keys[1].k_key.lk_keyid;
346                 ocapa->c_bvalid = 1;
347                 spin_unlock(&filter->fo_capa_lock);
348         }
349         goto verify;
350 out:
351         RETURN(rc);
352 }