Whamcloud - gitweb
09208b966723b8c563c1f8f2c333f15d42f0baf2
[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;
219         __u8 hmac_key[CAPA_KEY_LEN];
220         int rc = 0;
221
222         /* capability is disabled */
223         if (filter->fo_capa_stat == 0)
224                 RETURN(0);
225
226         ENTRY;
227         if (capa == NULL) {
228                 CDEBUG(D_ERROR, "no capa has been passed\n");
229                 RETURN(-EACCES);
230         }
231
232         if (blacklist_check(capa->lc_uid)) {
233                 DEBUG_CAPA(D_ERROR, capa, "found in blacklist\n");
234                 RETURN(-EACCES);
235         }
236
237         if (cmd == OBD_BRW_WRITE && !(capa->lc_op & (CAPA_WRITE | CAPA_TRUNC))) {
238                 DEBUG_CAPA(D_ERROR, capa, "have no write access\n");
239                 RETURN(-EACCES);
240         }
241         if (cmd == OBD_BRW_READ && !(capa->lc_op & (CAPA_WRITE | CAPA_READ))) {
242                 DEBUG_CAPA(D_ERROR, capa, "have no read access\n");
243                 RETURN(-EACCES);
244         }
245
246         if (OBD_FAIL_CHECK(OBD_FAIL_FILTER_VERIFY_CAPA))
247                 RETURN(-EACCES);
248
249         if (capa_expired(capa)) {
250                 DEBUG_CAPA(D_INFO | D_ERROR, capa, "expired");
251                 RETURN(-ESTALE);
252         }
253
254         ocapa = capa_get(capa->lc_uid, capa->lc_op, capa->lc_mdsid,
255                          capa->lc_ino, FILTER_CAPA);
256 verify:
257         if (ocapa) {
258                 /* fo_capa_lock protects capa too */
259                 spin_lock(&filter->fo_capa_lock);
260                 if (capa->lc_keyid == ocapa->c_capa.lc_keyid) {
261                         rc = memcmp(capa, &ocapa->c_capa, sizeof(*capa));
262                 } else if (ocapa->c_bvalid &&
263                            capa->lc_keyid == ocapa->c_bkeyid) {
264                         rc = memcmp(capa->lc_hmac, ocapa->c_bhmac,
265                                     sizeof(capa->lc_hmac));
266                 } else {
267                         /* ocapa is obsolete too */
268                         ocapa->c_bvalid = 0;
269                         goto new_capa;
270                 }
271
272                 if (rc && __capa_is_to_expire(ocapa)) {
273                         /* client should use new expiry now */
274                         ocapa->c_bvalid = 0;
275                         goto new_capa;
276                 }
277                 spin_unlock(&filter->fo_capa_lock);
278
279                 capa_put(ocapa);
280                 RETURN(rc ? -EACCES : 0);
281         }
282
283         spin_lock(&filter->fo_capa_lock);
284 new_capa:
285         list_for_each_entry(tmp, &filter->fo_capa_keys, k_list) {
286                 if (tmp->k_key.lk_mdsid == capa->lc_mdsid) {
287                         if (rkey == NULL)
288                                 rkey = tmp;
289                         else
290                                 bkey = tmp;
291                 }
292         }
293
294         if (rkey && bkey && capa_key_cmp(&rkey->k_key, &bkey->k_key) < 0) {
295                 tmp = rkey;
296                 rkey = bkey;
297                 bkey = tmp;
298         }
299
300         if ((!rkey || rkey->k_key.lk_keyid != capa->lc_keyid) &&
301             (!bkey || bkey->k_key.lk_keyid != capa->lc_keyid)) {
302                 spin_unlock(&filter->fo_capa_lock);
303                 GOTO(out, rc = -ESTALE);
304         }
305
306         LASSERT(rkey);
307
308         memcpy(&tcapa, capa, sizeof(tcapa));
309         tcapa.lc_keyid = rkey->k_key.lk_keyid;
310         memcpy(hmac_key, rkey->k_key.lk_key, sizeof(hmac_key));
311         spin_unlock(&filter->fo_capa_lock);
312
313         capa_hmac(filter->fo_capa_hmac, hmac_key, &tcapa);
314
315         /* store in capa cache */
316         ocapa = capa_renew(capa, FILTER_CAPA);
317         if (!ocapa)
318                 GOTO(out, rc = -ENOMEM);
319
320         if (bkey) {
321                 spin_lock(&filter->fo_capa_lock);
322                 tcapa.lc_keyid = bkey->k_key.lk_keyid;
323                 memcpy(hmac_key, bkey->k_key.lk_key, sizeof(hmac_key));
324                 ocapa->c_bkeyid = bkey->k_key.lk_keyid;
325                 spin_unlock(&filter->fo_capa_lock);
326
327                 capa_hmac(filter->fo_capa_hmac, bkey->k_key.lk_key, &tcapa);
328
329                 spin_lock(&filter->fo_capa_lock);
330                 memcpy(ocapa->c_bhmac, tcapa.lc_hmac, sizeof(ocapa->c_bhmac));
331                 ocapa->c_bvalid = 1;
332                 spin_unlock(&filter->fo_capa_lock);
333         }
334         goto verify;
335 out:
336         RETURN(rc);
337 }