Whamcloud - gitweb
LU-2275 mdt: Avoid setting positive dispositions too early
[fs/lustre-release.git] / lustre / mdt / mdt_idmap.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_idmap.c
37  *
38  * Author: Lai Siyao <lsy@clusterfs.com>
39  * Author: Fan Yong <fanyong@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/mm.h>
47 #include <linux/kmod.h>
48 #include <linux/string.h>
49 #include <linux/stat.h>
50 #include <linux/errno.h>
51 #include <linux/version.h>
52 #include <linux/unistd.h>
53 #include <asm/uaccess.h>
54 #include <linux/fs.h>
55 #include <linux/stat.h>
56 #include <asm/uaccess.h>
57 #include <linux/slab.h>
58
59 #include <libcfs/libcfs.h>
60 #include <libcfs/lucache.h>
61 #include <obd.h>
62 #include <obd_class.h>
63 #include <obd_support.h>
64 #include <lustre_net.h>
65 #include <lustre_import.h>
66 #include <lustre_dlm.h>
67 #include <lustre_sec.h>
68 #include <lustre_lib.h>
69
70 #include "mdt_internal.h"
71
72 #define mdt_init_sec_none(reply, exp)                                   \
73 do {                                                                    \
74         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |          \
75                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
76                                       OBD_CONNECT_MDS_CAPA |            \
77                                       OBD_CONNECT_OSS_CAPA);            \
78 } while (0)
79
80 int mdt_init_sec_level(struct mdt_thread_info *info)
81 {
82         struct mdt_device *mdt = info->mti_mdt;
83         struct ptlrpc_request *req = mdt_info_req(info);
84         char *client = libcfs_nid2str(req->rq_peer.nid);
85         struct obd_export *exp = req->rq_export;
86         struct obd_device *obd = exp->exp_obd;
87         struct obd_connect_data *data, *reply;
88         int rc = 0, remote;
89         ENTRY;
90
91         data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
92         reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
93         if (data == NULL || reply == NULL)
94                 RETURN(-EFAULT);
95
96         /* connection from MDT is always trusted */
97         if (req->rq_auth_usr_mdt) {
98                 mdt_init_sec_none(reply, exp);
99                 RETURN(0);
100         }
101
102         /* no GSS support case */
103         if (!req->rq_auth_gss) {
104                 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
105                         CWARN("client %s -> target %s does not user GSS, "
106                               "can not run under security level %d.\n",
107                               client, obd->obd_name, mdt->mdt_sec_level);
108                         RETURN(-EACCES);
109                 } else {
110                         mdt_init_sec_none(reply, exp);
111                         RETURN(0);
112                 }
113         }
114
115         /* old version case */
116         if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
117                      !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
118                      !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
119                 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
120                         CWARN("client %s -> target %s uses old version, "
121                               "can not run under security level %d.\n",
122                               client, obd->obd_name, mdt->mdt_sec_level);
123                         RETURN(-EACCES);
124                 } else {
125                         CWARN("client %s -> target %s uses old version, "
126                               "run under security level %d.\n",
127                               client, obd->obd_name, mdt->mdt_sec_level);
128                         mdt_init_sec_none(reply, exp);
129                         RETURN(0);
130                 }
131         }
132
133         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
134         if (remote) {
135                 if (!req->rq_auth_remote)
136                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
137                                "asked to be remote.\n", client, obd->obd_name);
138         } else if (req->rq_auth_remote) {
139                 remote = 1;
140                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
141                        "as remote by default.\n", client, obd->obd_name);
142         }
143
144         if (remote) {
145                 if (!mdt->mdt_opts.mo_oss_capa) {
146                         CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
147                                " but OSS capabilities are not enabled: %d.\n",
148                                client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
149                         RETURN(-EACCES);
150                 }
151         } else {
152                 if (req->rq_auth_uid == INVALID_UID) {
153                         CDEBUG(D_SEC, "client %s -> target %s: user is not "
154                                "authenticated!\n", client, obd->obd_name);
155                         RETURN(-EACCES);
156                 }
157         }
158
159         switch (mdt->mdt_sec_level) {
160         case LUSTRE_SEC_NONE:
161                 if (!remote) {
162                         mdt_init_sec_none(reply, exp);
163                         break;
164                 } else {
165                         CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
166                                "can not run under security level %d.\n",
167                                client, obd->obd_name, mdt->mdt_sec_level);
168                         RETURN(-EACCES);
169                 }
170         case LUSTRE_SEC_REMOTE:
171                 if (!remote)
172                         mdt_init_sec_none(reply, exp);
173                 break;
174         case LUSTRE_SEC_ALL:
175                 if (!remote) {
176                         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
177                                                       OBD_CONNECT_RMT_CLIENT_FORCE);
178                         if (!mdt->mdt_opts.mo_mds_capa)
179                                 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
180                         if (!mdt->mdt_opts.mo_oss_capa)
181                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
182                 }
183                 break;
184         default:
185                 RETURN(-EINVAL);
186         }
187
188         RETURN(rc);
189 }
190
191 int mdt_init_idmap(struct mdt_thread_info *info)
192 {
193         struct ptlrpc_request *req = mdt_info_req(info);
194         struct mdt_export_data *med = mdt_req2med(req);
195         struct obd_export *exp = req->rq_export;
196         char *client = libcfs_nid2str(req->rq_peer.nid);
197         struct obd_device *obd = exp->exp_obd;
198         int rc = 0;
199         ENTRY;
200
201         if (exp_connect_rmtclient(exp)) {
202                 mutex_lock(&med->med_idmap_mutex);
203                 if (!med->med_idmap)
204                         med->med_idmap = lustre_idmap_init();
205                 mutex_unlock(&med->med_idmap_mutex);
206
207                 if (IS_ERR(med->med_idmap)) {
208                         long err = PTR_ERR(med->med_idmap);
209
210                         med->med_idmap = NULL;
211                         CERROR("client %s -> target %s "
212                                "failed to init idmap [%ld]!\n",
213                                client, obd->obd_name, err);
214                         RETURN(err);
215                 } else if (!med->med_idmap) {
216                         CERROR("client %s -> target %s "
217                                "failed to init(2) idmap!\n",
218                                client, obd->obd_name);
219                         RETURN(-ENOMEM);
220                 }
221
222                 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
223                        client, obd->obd_name);
224                 /* NB, MDS_CONNECT establish root idmap too! */
225                 rc = mdt_handle_idmap(info);
226         }
227         RETURN(rc);
228 }
229
230 void mdt_cleanup_idmap(struct mdt_export_data *med)
231 {
232         mutex_lock(&med->med_idmap_mutex);
233         if (med->med_idmap != NULL) {
234                 lustre_idmap_fini(med->med_idmap);
235                 med->med_idmap = NULL;
236         }
237         mutex_unlock(&med->med_idmap_mutex);
238 }
239
240 static inline void mdt_revoke_export_locks(struct obd_export *exp)
241 {
242         /* don't revoke locks during recovery */
243         if (exp->exp_obd->obd_recovering)
244                 return;
245
246         ldlm_revoke_export_locks(exp);
247 }
248
249 int mdt_handle_idmap(struct mdt_thread_info *info)
250 {
251         struct ptlrpc_request *req = mdt_info_req(info);
252         struct mdt_device *mdt = info->mti_mdt;
253         struct mdt_export_data *med;
254         struct ptlrpc_user_desc *pud = req->rq_user_desc;
255         struct md_identity *identity;
256         __u32 opc;
257         int rc = 0;
258         ENTRY;
259
260         if (!req->rq_export)
261                 RETURN(0);
262
263         med = mdt_req2med(req);
264         if (!exp_connect_rmtclient(info->mti_exp))
265                 RETURN(0);
266
267         opc = lustre_msg_get_opc(req->rq_reqmsg);
268         /* Bypass other opc */
269         if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
270             (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
271                 RETURN(0);
272
273         LASSERT(med->med_idmap);
274
275         if (unlikely(!pud)) {
276                 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
277                        "present\n");
278                 RETURN(-EACCES);
279         }
280
281         if (req->rq_auth_mapped_uid == INVALID_UID) {
282                 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
283                        "/etc/lustre/idmap.conf!\n");
284                 RETURN(-EACCES);
285         }
286
287         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
288                 CDEBUG(D_SEC, "remote client must run with identity_get "
289                        "enabled!\n");
290                 RETURN(-EACCES);
291         }
292
293         identity = mdt_identity_get(mdt->mdt_identity_cache,
294                                     req->rq_auth_mapped_uid);
295         if (IS_ERR(identity)) {
296                 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
297                        req->rq_auth_mapped_uid);
298                 RETURN(-EACCES);
299         }
300
301         switch (opc) {
302                 case SEC_CTX_INIT:
303                 case SEC_CTX_INIT_CONT:
304                 case MDS_CONNECT:
305                         rc = lustre_idmap_add(med->med_idmap,
306                                               pud->pud_uid, identity->mi_uid,
307                                               pud->pud_gid, identity->mi_gid);
308                         break;
309                 case SEC_CTX_FINI:
310                         rc = lustre_idmap_del(med->med_idmap,
311                                               pud->pud_uid, identity->mi_uid,
312                                               pud->pud_gid, identity->mi_gid);
313                         break;
314         }
315
316         mdt_identity_put(mdt->mdt_identity_cache, identity);
317
318         if (rc)
319                 RETURN(rc);
320
321         switch (opc) {
322                 case SEC_CTX_INIT:
323                 case SEC_CTX_INIT_CONT:
324                 case SEC_CTX_FINI:
325                         mdt_revoke_export_locks(req->rq_export);
326                         break;
327         }
328
329         RETURN(0);
330 }
331
332 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
333                               struct ptlrpc_user_desc *pud)
334 {
335         struct mdt_export_data    *med = mdt_req2med(req);
336         struct lustre_idmap_table *idmap = med->med_idmap;
337         uid_t uid, fsuid;
338         gid_t gid, fsgid;
339
340         /* Only remote client need desc_to_idmap. */
341         if (!exp_connect_rmtclient(req->rq_export))
342                 return 0;
343
344         uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
345         if (uid == CFS_IDMAP_NOTFOUND) {
346                 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
347                 return -EACCES;
348         }
349
350         if (pud->pud_uid == pud->pud_fsuid) {
351                 fsuid = uid;
352         } else {
353                 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
354                 if (fsuid == CFS_IDMAP_NOTFOUND) {
355                         CDEBUG(D_SEC, "no mapping for fsuid %u\n",
356                                pud->pud_fsuid);
357                         return -EACCES;
358                 }
359         }
360
361         gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
362         if (gid == CFS_IDMAP_NOTFOUND) {
363                 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
364                 return -EACCES;
365         }
366
367         if (pud->pud_gid == pud->pud_fsgid) {
368                 fsgid = gid;
369         } else {
370                 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
371                 if (fsgid == CFS_IDMAP_NOTFOUND) {
372                         CDEBUG(D_SEC, "no mapping for fsgid %u\n",
373                                pud->pud_fsgid);
374                         return -EACCES;
375                 }
376         }
377
378         pud->pud_uid = uid;
379         pud->pud_gid = gid;
380         pud->pud_fsuid = fsuid;
381         pud->pud_fsgid = fsgid;
382
383         return 0;
384 }
385
386 /*
387  * Reverse mapping
388  */
389 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
390 {
391         struct ptlrpc_request     *req = mdt_info_req(info);
392         struct lu_ucred           *uc = mdt_ucred(info);
393         struct mdt_export_data    *med = mdt_req2med(req);
394         struct lustre_idmap_table *idmap = med->med_idmap;
395
396         if (!exp_connect_rmtclient(info->mti_exp))
397                 return;
398
399         if (body->valid & OBD_MD_FLUID) {
400                 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
401
402                 if (uid == CFS_IDMAP_NOTFOUND) {
403                         uid = NOBODY_UID;
404                         if (body->valid & OBD_MD_FLMODE)
405                                 body->mode = (body->mode & ~S_IRWXU) |
406                                              ((body->mode & S_IRWXO) << 6);
407                 }
408
409                 body->uid = uid;
410         }
411
412         if (body->valid & OBD_MD_FLGID) {
413                 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
414
415                 if (gid == CFS_IDMAP_NOTFOUND) {
416                         gid = NOBODY_GID;
417                         if (body->valid & OBD_MD_FLMODE)
418                                 body->mode = (body->mode & ~S_IRWXG) |
419                                              ((body->mode & S_IRWXO) << 3);
420                 }
421
422                 body->gid = gid;
423         }
424 }
425
426 /* Do not ignore root_squash for non-setattr case. */
427 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
428 {
429         struct ptlrpc_request     *req = mdt_info_req(info);
430         struct lu_ucred           *uc = mdt_ucred_check(info);
431         struct lu_attr            *attr = &info->mti_attr.ma_attr;
432         struct mdt_export_data    *med = mdt_req2med(req);
433         struct lustre_idmap_table *idmap = med->med_idmap;
434
435         if (uc == NULL)
436                 return -EINVAL;
437
438         if (op != REINT_SETATTR) {
439                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
440                         attr->la_uid = uc->uc_fsuid;
441                 /* for S_ISGID, inherit gid from his parent, such work will be
442                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
443                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
444                         attr->la_gid = uc->uc_fsgid;
445         } else if (exp_connect_rmtclient(info->mti_exp)) {
446                 /* NB: -1 case will be handled by mdt_fix_attr() later. */
447                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
448                         uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
449                                                             attr->la_uid);
450
451                         if (uid == CFS_IDMAP_NOTFOUND) {
452                                 CDEBUG(D_SEC, "Deny chown to uid %u\n",
453                                        attr->la_uid);
454                                 return -EPERM;
455                         }
456
457                         attr->la_uid = uid;
458                 }
459                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
460                         gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
461                                                             attr->la_gid);
462
463                         if (gid == CFS_IDMAP_NOTFOUND) {
464                                 CDEBUG(D_SEC, "Deny chown to gid %u\n",
465                                        attr->la_gid);
466                                 return -EPERM;
467                         }
468
469                         attr->la_gid = gid;
470                 }
471         }
472
473         return 0;
474 }