Whamcloud - gitweb
LU-1175 tests: use ofd by default instead of obdfilter
[fs/lustre-release.git] / lustre / obdfilter / filter_capa.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdfilter/filter_capa.c
35  *
36  * Author: Lai Siyao <lsy@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_FILTER
40
41 #include <linux/fs.h>
42 #include <linux/version.h>
43 #include <asm/uaccess.h>
44 #include <linux/file.h>
45 #include <linux/kmod.h>
46
47 #include <lustre_fsfilt.h>
48 #include <lustre_capa.h>
49
50 #include "filter_internal.h"
51
52 static inline __u32 filter_ck_keyid(struct filter_capa_key *key)
53 {
54         return key->k_key.lk_keyid;
55 }
56
57 int filter_update_capa_key(struct obd_device *obd, struct lustre_capa_key *new)
58 {
59         struct filter_obd *filter = &obd->u.filter;
60         struct filter_capa_key *k, *keys[2] = { NULL, NULL };
61         int i;
62
63         cfs_spin_lock(&capa_lock);
64         cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
65                 if (k->k_key.lk_seq != new->lk_seq)
66                         continue;
67
68                 if (keys[0]) {
69                         keys[1] = k;
70                         if (filter_ck_keyid(keys[1]) > filter_ck_keyid(keys[0]))
71                                 keys[1] = keys[0], keys[0] = k;
72                 } else {
73                         keys[0] = k;
74                 }
75         }
76         cfs_spin_unlock(&capa_lock);
77
78         for (i = 0; i < 2; i++) {
79                 if (!keys[i])
80                         continue;
81                 if (filter_ck_keyid(keys[i]) != new->lk_keyid)
82                         continue;
83                 /* maybe because of recovery or other reasons, MDS sent the
84                  * the old capability key again.
85                  */
86                 cfs_spin_lock(&capa_lock);
87                 keys[i]->k_key = *new;
88                 cfs_spin_unlock(&capa_lock);
89
90                 RETURN(0);
91         }
92
93         if (keys[1]) {
94                 /* if OSS already have two keys, update the old one */
95                 k = keys[1];
96         } else {
97                 OBD_ALLOC_PTR(k);
98                 if (!k)
99                         RETURN(-ENOMEM);
100                 CFS_INIT_LIST_HEAD(&k->k_list);
101         }
102
103         cfs_spin_lock(&capa_lock);
104         k->k_key = *new;
105         if (cfs_list_empty(&k->k_list))
106                 cfs_list_add(&k->k_list, &filter->fo_capa_keys);
107         cfs_spin_unlock(&capa_lock);
108
109         DEBUG_CAPA_KEY(D_SEC, new, "new");
110         RETURN(0);
111 }
112
113 int filter_auth_capa(struct obd_export *exp,
114                      struct lu_fid *fid, obd_seq seq,
115                      struct lustre_capa *capa, __u64 opc)
116 {
117         struct obd_device *obd = exp->exp_obd;
118         struct filter_obd *filter = &obd->u.filter;
119         struct filter_capa_key *k;
120         struct lustre_capa_key key;
121         struct obd_capa *oc;
122         __u8 *hmac;
123         int keys_ready = 0, key_found = 0, rc = 0;
124         ENTRY;
125
126         /* skip capa check for llog and obdecho */
127         if (!fid_seq_is_mdt(seq))
128                 RETURN(0);
129
130         if (capa == BYPASS_CAPA)
131                 RETURN(0);
132
133         /* capability is disabled */
134         if (!filter->fo_fl_oss_capa)
135                 RETURN(0);
136
137         if (!(exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
138                 RETURN(0);
139
140         if (capa == NULL) {
141                 if (fid)
142                         CERROR("seq/fid/opc "LPU64"/"DFID"/"LPX64
143                                ": no capability has been passed\n",
144                                seq, PFID(fid), opc);
145                 else
146                         CERROR("seq/opc "LPU64"/"LPX64
147                                ": no capability has been passed\n",
148                                seq, opc);
149                 RETURN(-EACCES);
150         }
151
152         if (opc == CAPA_OPC_OSS_READ) {
153                 if (!(capa->lc_opc & CAPA_OPC_OSS_RW))
154                         rc = -EACCES;
155         } else if (!capa_opc_supported(capa, opc)) {
156                 rc = -EACCES;
157         }
158         if (rc) {
159                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
160                 RETURN(rc);
161         }
162
163         oc = capa_lookup(filter->fo_capa_hash, capa, 0);
164         if (oc) {
165                 cfs_spin_lock(&oc->c_lock);
166                 if (capa_is_expired(oc)) {
167                         DEBUG_CAPA(D_ERROR, capa, "expired");
168                         rc = -ESTALE;
169                 }
170                 cfs_spin_unlock(&oc->c_lock);
171
172                 capa_put(oc);
173                 RETURN(rc);
174         }
175
176         if (capa_is_expired_sec(capa)) {
177                 DEBUG_CAPA(D_ERROR, capa, "expired");
178                 RETURN(-ESTALE);
179         }
180
181         cfs_spin_lock(&capa_lock);
182         cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
183                 if (k->k_key.lk_seq == seq) {
184                         keys_ready = 1;
185                         if (k->k_key.lk_keyid == capa_keyid(capa)) {
186                                 key = k->k_key;
187                                 key_found = 1;
188                                 break;
189                         }
190                 }
191         }
192         cfs_spin_unlock(&capa_lock);
193
194         if (!keys_ready) {
195                 CDEBUG(D_SEC, "MDS hasn't propagated capability keys yet, "
196                        "ignore check!\n");
197                 RETURN(0);
198         }
199
200        if (!key_found) {
201                 DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
202                 RETURN(-ESTALE);
203         }
204
205         OBD_ALLOC(hmac, CAPA_HMAC_MAX_LEN);
206         if (hmac == NULL)
207                 RETURN(-ENOMEM);
208
209         rc = capa_hmac(hmac, capa, key.lk_key);
210         if (rc) {
211                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: rc %d", rc);
212                 OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
213                 RETURN(rc);
214         }
215
216         rc = memcmp(hmac, capa->lc_hmac, CAPA_HMAC_MAX_LEN);
217         OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
218         if (rc) {
219                 DEBUG_CAPA_KEY(D_ERROR, &key, "calculate HMAC with ");
220                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
221                 RETURN(-EACCES);
222         }
223
224         /* store in capa hash */
225         oc = capa_add(filter->fo_capa_hash, capa);
226         capa_put(oc);
227         RETURN(0);
228 }
229
230 int filter_capa_fixoa(struct obd_export *exp, struct obdo *oa, obd_seq seq,
231                       struct lustre_capa *capa)
232 {
233         struct obd_device       *obd = exp->exp_obd;
234         struct filter_obd       *filter = &obd->u.filter;
235         int                     rc = 0;
236         ENTRY;
237
238         /* skip capa check for llog and obdecho */
239         if (!fid_seq_is_mdt(seq))
240                 RETURN(0);
241
242         if (!(exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
243                 RETURN(0);
244
245         /* capability is disabled */
246         if (!filter->fo_fl_oss_capa)
247                 RETURN(0);
248
249         if (unlikely(!capa))
250                 RETURN(-EACCES);
251
252         if (capa_flags(capa) == LC_ID_CONVERT) {
253                 struct filter_capa_key *k;
254                 int found = 0;
255
256                 cfs_spin_lock(&capa_lock);
257                 cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
258                         if (k->k_key.lk_seq == seq &&
259                             k->k_key.lk_keyid == capa_keyid(capa)) {
260                                 found = 1;
261                                 break;
262                         }
263                 }
264                 cfs_spin_unlock(&capa_lock);
265
266                 if (found) {
267                         union {
268                                 __u64 id64;
269                                 __u32 id32[2];
270                         } uid, gid;
271                         __u32 d[4], s[4];
272
273                         uid.id64 = capa_uid(capa);
274                         gid.id64 = capa_gid(capa);
275                         s[0] = uid.id32[0];
276                         s[1] = uid.id32[1];
277                         s[2] = gid.id32[0];
278                         s[3] = gid.id32[1];
279
280                         rc = capa_decrypt_id(d, s, k->k_key.lk_key,
281                                              CAPA_HMAC_KEY_MAX_LEN);
282                         if (unlikely(rc))
283                                 RETURN(rc);
284
285                         oa->o_uid = d[0];
286                         oa->o_gid = d[2];
287                 } else {
288                         DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
289                         rc = -ESTALE;
290                 }
291         }
292
293         RETURN(rc);
294 }
295
296 void filter_free_capa_keys(struct filter_obd *filter)
297 {
298         struct filter_capa_key *key, *n;
299
300         cfs_spin_lock(&capa_lock);
301         cfs_list_for_each_entry_safe(key, n, &filter->fo_capa_keys, k_list) {
302                 cfs_list_del_init(&key->k_list);
303                 OBD_FREE(key, sizeof(*key));
304         }
305         cfs_spin_unlock(&capa_lock);
306 }