Whamcloud - gitweb
LU-6158 mdt: always shrink_capsule in getxattr_all
[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, 2013, 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/user_namespace.h>
49 #ifdef HAVE_UIDGID_HEADER
50 # include <linux/uidgid.h>
51 #endif
52 #include <linux/string.h>
53 #include <linux/stat.h>
54 #include <linux/errno.h>
55 #include <linux/version.h>
56 #include <linux/unistd.h>
57 #include <asm/uaccess.h>
58 #include <linux/fs.h>
59 #include <linux/stat.h>
60 #include <asm/uaccess.h>
61 #include <linux/slab.h>
62
63 #include <libcfs/libcfs.h>
64 #include <obd.h>
65 #include <obd_class.h>
66 #include <obd_support.h>
67 #include <lustre_net.h>
68 #include <lustre_import.h>
69 #include <lustre_dlm.h>
70 #include <lustre_sec.h>
71 #include <lustre_lib.h>
72
73 #include "mdt_internal.h"
74
75 int mdt_init_idmap(struct tgt_session_info *tsi)
76 {
77         struct ptlrpc_request   *req = tgt_ses_req(tsi);
78         struct mdt_export_data *med = mdt_req2med(req);
79         struct obd_export *exp = req->rq_export;
80         char *client = libcfs_nid2str(req->rq_peer.nid);
81         int rc = 0;
82         ENTRY;
83
84         if (exp_connect_rmtclient(exp)) {
85                 mutex_lock(&med->med_idmap_mutex);
86                 if (!med->med_idmap)
87                         med->med_idmap = lustre_idmap_init();
88                 mutex_unlock(&med->med_idmap_mutex);
89
90                 if (IS_ERR(med->med_idmap)) {
91                         long err = PTR_ERR(med->med_idmap);
92
93                         med->med_idmap = NULL;
94                         CERROR("%s: client %s -> target %s "
95                                "failed to init idmap [%ld]!\n",
96                                tgt_name(tsi->tsi_tgt), client,
97                                tgt_name(tsi->tsi_tgt), err);
98                         RETURN(err);
99                 } else if (!med->med_idmap) {
100                         CERROR("%s: client %s -> target %s "
101                                "failed to init(2) idmap!\n",
102                                tgt_name(tsi->tsi_tgt), client,
103                                tgt_name(tsi->tsi_tgt));
104                         RETURN(-ENOMEM);
105                 }
106
107                 CDEBUG(D_SEC, "%s: client %s -> target %s is remote.\n",
108                         tgt_name(tsi->tsi_tgt), client,
109                         tgt_name(tsi->tsi_tgt));
110                 /* NB, MDS_CONNECT establish root idmap too! */
111                 rc = mdt_handle_idmap(tsi);
112         }
113         RETURN(rc);
114 }
115
116 void mdt_cleanup_idmap(struct mdt_export_data *med)
117 {
118         mutex_lock(&med->med_idmap_mutex);
119         if (med->med_idmap != NULL) {
120                 lustre_idmap_fini(med->med_idmap);
121                 med->med_idmap = NULL;
122         }
123         mutex_unlock(&med->med_idmap_mutex);
124 }
125
126 static inline void mdt_revoke_export_locks(struct obd_export *exp)
127 {
128         /* don't revoke locks during recovery */
129         if (exp->exp_obd->obd_recovering)
130                 return;
131
132         ldlm_revoke_export_locks(exp);
133 }
134
135 int mdt_handle_idmap(struct tgt_session_info *tsi)
136 {
137         struct ptlrpc_request   *req = tgt_ses_req(tsi);
138         struct mdt_device       *mdt = mdt_exp2dev(req->rq_export);
139         struct mdt_export_data *med;
140         struct ptlrpc_user_desc *pud = req->rq_user_desc;
141         struct md_identity *identity;
142         __u32 opc;
143         int rc = 0;
144         ENTRY;
145
146         if (!req->rq_export)
147                 RETURN(0);
148
149         med = mdt_req2med(req);
150         if (!exp_connect_rmtclient(req->rq_export))
151                 RETURN(0);
152
153         opc = lustre_msg_get_opc(req->rq_reqmsg);
154         /* Bypass other opc */
155         if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
156             (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
157                 RETURN(0);
158
159         LASSERT(med->med_idmap);
160
161         if (unlikely(!pud)) {
162                 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
163                        "present\n");
164                 RETURN(-EACCES);
165         }
166
167         if (!uid_valid(make_kuid(&init_user_ns, req->rq_auth_mapped_uid))) {
168                 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
169                        "/etc/lustre/idmap.conf!\n");
170                 RETURN(-EACCES);
171         }
172
173         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
174                 CDEBUG(D_SEC, "remote client must run with identity_get "
175                        "enabled!\n");
176                 RETURN(-EACCES);
177         }
178
179         identity = mdt_identity_get(mdt->mdt_identity_cache,
180                                     req->rq_auth_mapped_uid);
181         if (IS_ERR(identity)) {
182                 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
183                        req->rq_auth_mapped_uid);
184                 RETURN(-EACCES);
185         }
186
187         switch (opc) {
188                 case SEC_CTX_INIT:
189                 case SEC_CTX_INIT_CONT:
190                 case MDS_CONNECT:
191                         rc = lustre_idmap_add(med->med_idmap,
192                                               pud->pud_uid, identity->mi_uid,
193                                               pud->pud_gid, identity->mi_gid);
194                         break;
195                 case SEC_CTX_FINI:
196                         rc = lustre_idmap_del(med->med_idmap,
197                                               pud->pud_uid, identity->mi_uid,
198                                               pud->pud_gid, identity->mi_gid);
199                         break;
200         }
201
202         mdt_identity_put(mdt->mdt_identity_cache, identity);
203
204         if (rc)
205                 RETURN(rc);
206
207         switch (opc) {
208                 case SEC_CTX_INIT:
209                 case SEC_CTX_INIT_CONT:
210                 case SEC_CTX_FINI:
211                         mdt_revoke_export_locks(req->rq_export);
212                         break;
213         }
214
215         RETURN(0);
216 }
217
218 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
219                               struct ptlrpc_user_desc *pud)
220 {
221         struct mdt_export_data    *med = mdt_req2med(req);
222         struct lustre_idmap_table *idmap = med->med_idmap;
223         uid_t uid, fsuid;
224         gid_t gid, fsgid;
225
226         /* Only remote client need desc_to_idmap. */
227         if (!exp_connect_rmtclient(req->rq_export))
228                 return 0;
229
230         uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
231         if (uid == CFS_IDMAP_NOTFOUND) {
232                 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
233                 return -EACCES;
234         }
235
236         if (pud->pud_uid == pud->pud_fsuid) {
237                 fsuid = uid;
238         } else {
239                 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
240                 if (fsuid == CFS_IDMAP_NOTFOUND) {
241                         CDEBUG(D_SEC, "no mapping for fsuid %u\n",
242                                pud->pud_fsuid);
243                         return -EACCES;
244                 }
245         }
246
247         gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
248         if (gid == CFS_IDMAP_NOTFOUND) {
249                 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
250                 return -EACCES;
251         }
252
253         if (pud->pud_gid == pud->pud_fsgid) {
254                 fsgid = gid;
255         } else {
256                 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
257                 if (fsgid == CFS_IDMAP_NOTFOUND) {
258                         CDEBUG(D_SEC, "no mapping for fsgid %u\n",
259                                pud->pud_fsgid);
260                         return -EACCES;
261                 }
262         }
263
264         pud->pud_uid = uid;
265         pud->pud_gid = gid;
266         pud->pud_fsuid = fsuid;
267         pud->pud_fsgid = fsgid;
268
269         return 0;
270 }
271
272 /*
273  * Reverse mapping
274  */
275 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
276 {
277         struct ptlrpc_request     *req = mdt_info_req(info);
278         struct lu_ucred           *uc = mdt_ucred(info);
279         struct mdt_export_data    *med = mdt_req2med(req);
280         struct lustre_idmap_table *idmap = med->med_idmap;
281
282         if (!exp_connect_rmtclient(info->mti_exp))
283                 return;
284
285         if (body->mbo_valid & OBD_MD_FLUID) {
286                 uid_t uid;
287
288                 uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->mbo_uid);
289
290                 if (uid == CFS_IDMAP_NOTFOUND) {
291                         uid = NOBODY_UID;
292                         if (body->mbo_valid & OBD_MD_FLMODE)
293                                 body->mbo_mode = (body->mbo_mode & ~S_IRWXU) |
294                                              ((body->mbo_mode & S_IRWXO) << 6);
295                 }
296
297                 body->mbo_uid = uid;
298         }
299
300         if (body->mbo_valid & OBD_MD_FLGID) {
301                 gid_t gid;
302
303                 gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->mbo_gid);
304
305                 if (gid == CFS_IDMAP_NOTFOUND) {
306                         gid = NOBODY_GID;
307                         if (body->mbo_valid & OBD_MD_FLMODE)
308                                 body->mbo_mode = (body->mbo_mode & ~S_IRWXG) |
309                                              ((body->mbo_mode & S_IRWXO) << 3);
310                 }
311
312                 body->mbo_gid = gid;
313         }
314 }
315
316 /* Do not ignore root_squash for non-setattr case. */
317 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
318 {
319         struct ptlrpc_request     *req = mdt_info_req(info);
320         struct lu_ucred           *uc = mdt_ucred_check(info);
321         struct lu_attr            *attr = &info->mti_attr.ma_attr;
322         struct mdt_export_data    *med = mdt_req2med(req);
323         struct lustre_idmap_table *idmap = med->med_idmap;
324
325         if (uc == NULL)
326                 return -EINVAL;
327
328         if (op != REINT_SETATTR) {
329                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
330                         attr->la_uid = uc->uc_fsuid;
331                 /* for S_ISGID, inherit gid from his parent, such work will be
332                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
333                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
334                         attr->la_gid = uc->uc_fsgid;
335         } else if (exp_connect_rmtclient(info->mti_exp)) {
336                 /* NB: -1 case will be handled by mdt_fix_attr() later. */
337                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
338                         uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
339                                                             attr->la_uid);
340
341                         if (uid == CFS_IDMAP_NOTFOUND) {
342                                 CDEBUG(D_SEC, "Deny chown to uid %u\n",
343                                        attr->la_uid);
344                                 return -EPERM;
345                         }
346
347                         attr->la_uid = uid;
348                 }
349                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
350                         gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
351                                                             attr->la_gid);
352
353                         if (gid == CFS_IDMAP_NOTFOUND) {
354                                 CDEBUG(D_SEC, "Deny chown to gid %u\n",
355                                        attr->la_gid);
356                                 return -EPERM;
357                         }
358
359                         attr->la_gid = gid;
360                 }
361         }
362
363         return 0;
364 }