Whamcloud - gitweb
LU-16741 ptlrpc: rename ptlrpc_req_finished for component ptlrpc
[fs/lustre-release.git] / lustre / ptlrpc / gss / lproc_gss.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_SEC
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/dcache.h>
37 #include <linux/fs.h>
38 #include <linux/mutex.h>
39
40 #include <obd.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <lustre_import.h>
45 #include <lprocfs_status.h>
46 #include <lustre_sec.h>
47
48 #include "gss_err.h"
49 #include "gss_internal.h"
50 #include "gss_api.h"
51
52 static struct dentry *gss_debugfs_dir_lk;
53 static struct dentry *gss_debugfs_dir;
54 static struct proc_dir_entry *gss_lprocfs_dir;
55
56 /*
57  * statistic of "out-of-sequence-window"
58  */
59 static struct {
60         spinlock_t      oos_lock;
61         atomic_t        oos_cli_count;          /* client occurrence */
62         int             oos_cli_behind;         /* client max seqs behind */
63         atomic_t        oos_svc_replay[3];      /* server replay detected */
64         atomic_t        oos_svc_pass[3];        /* server verified ok */
65 } gss_stat_oos = {
66         .oos_cli_count  = ATOMIC_INIT(0),
67         .oos_cli_behind = 0,
68         .oos_svc_replay = { ATOMIC_INIT(0), },
69         .oos_svc_pass   = { ATOMIC_INIT(0), },
70 };
71
72 void gss_stat_oos_record_cli(int behind)
73 {
74         atomic_inc(&gss_stat_oos.oos_cli_count);
75
76         spin_lock(&gss_stat_oos.oos_lock);
77         if (behind > gss_stat_oos.oos_cli_behind)
78                 gss_stat_oos.oos_cli_behind = behind;
79         spin_unlock(&gss_stat_oos.oos_lock);
80 }
81
82 void gss_stat_oos_record_svc(int phase, int replay)
83 {
84         LASSERT(phase >= 0 && phase <= 2);
85
86         if (replay)
87                 atomic_inc(&gss_stat_oos.oos_svc_replay[phase]);
88         else
89                 atomic_inc(&gss_stat_oos.oos_svc_pass[phase]);
90 }
91
92 static int gss_proc_oos_seq_show(struct seq_file *m, void *v)
93 {
94         seq_printf(m, "seqwin:             %u\n"
95                    "backwin:            %u\n"
96                    "client fall behind seqwin\n"
97                    "  occurrence:       %d\n"
98                    "  max seq behind:   %d\n"
99                    "server replay detected:\n"
100                    "  phase 0:          %d\n"
101                    "  phase 1:          %d\n"
102                    "  phase 2:          %d\n"
103                    "server verify ok:\n"
104                    "  phase 2:          %d\n",
105                    GSS_SEQ_WIN_MAIN,
106                    GSS_SEQ_WIN_BACK,
107                    atomic_read(&gss_stat_oos.oos_cli_count),
108                    gss_stat_oos.oos_cli_behind,
109                    atomic_read(&gss_stat_oos.oos_svc_replay[0]),
110                    atomic_read(&gss_stat_oos.oos_svc_replay[1]),
111                    atomic_read(&gss_stat_oos.oos_svc_replay[2]),
112                    atomic_read(&gss_stat_oos.oos_svc_pass[2]));
113         return 0;
114 }
115 LDEBUGFS_SEQ_FOPS_RO(gss_proc_oos);
116
117 static ssize_t
118 gss_proc_write_secinit(struct file *file, const char *buffer,
119                                   size_t count, loff_t *off)
120 {
121         int rc;
122
123         rc = gss_do_ctx_init_rpc((char *) buffer, count);
124         if (rc) {
125                 LASSERT(rc < 0);
126                 return rc;
127         }
128         return count;
129 }
130
131 static const struct file_operations gss_proc_secinit = {
132         .write = gss_proc_write_secinit,
133 };
134
135 static int
136 sptlrpc_krb5_allow_old_client_csum_seq_show(struct seq_file *m,
137                                             void *data)
138 {
139         seq_printf(m, "%u\n", krb5_allow_old_client_csum);
140         return 0;
141 }
142
143 static ssize_t
144 sptlrpc_krb5_allow_old_client_csum_seq_write(struct file *file,
145                                              const char __user *buffer,
146                                              size_t count, loff_t *off)
147 {
148         bool val;
149         int rc;
150
151         rc = kstrtobool_from_user(buffer, count, &val);
152         if (rc)
153                 return rc;
154
155         krb5_allow_old_client_csum = val;
156         return count;
157 }
158 LPROC_SEQ_FOPS(sptlrpc_krb5_allow_old_client_csum);
159
160 #ifdef HAVE_GSS_KEYRING
161 static int sptlrpc_gss_check_upcall_ns_seq_show(struct seq_file *m, void *data)
162 {
163         seq_printf(m, "%u\n", gss_check_upcall_ns);
164         return 0;
165 }
166
167 static ssize_t sptlrpc_gss_check_upcall_ns_seq_write(struct file *file,
168                                                      const char __user *buffer,
169                                                      size_t count, loff_t *off)
170 {
171         bool val;
172         int rc;
173
174         rc = kstrtobool_from_user(buffer, count, &val);
175         if (rc)
176                 return rc;
177
178         gss_check_upcall_ns = val;
179         return count;
180 }
181 LPROC_SEQ_FOPS(sptlrpc_gss_check_upcall_ns);
182 #endif /* HAVE_GSS_KEYRING */
183
184 static int rsi_upcall_seq_show(struct seq_file *m,
185                                void *data)
186 {
187         down_read(&rsicache->uc_upcall_rwsem);
188         seq_printf(m, "%s\n", rsicache->uc_upcall);
189         up_read(&rsicache->uc_upcall_rwsem);
190
191         return 0;
192 }
193
194 static ssize_t rsi_upcall_seq_write(struct file *file,
195                                     const char __user *buffer,
196                                     size_t count, loff_t *off)
197 {
198         char *kbuf = NULL;
199         int rc;
200
201         OBD_ALLOC(kbuf, count + 1);
202         if (kbuf == NULL)
203                 return -ENOMEM;
204
205         if (copy_from_user(kbuf, buffer, count))
206                 GOTO(out, rc = -EFAULT);
207
208         kbuf[count] = '\0';
209
210         rc = upcall_cache_set_upcall(rsicache, kbuf, count, true);
211         if (rc) {
212                 CERROR("%s: incorrect rsi upcall %s. Valid value for sptlrpc.gss.rsi_upcall is an executable pathname: rc = %d\n",
213                        rsicache->uc_name, kbuf, rc);
214                 GOTO(out, rc);
215         }
216
217         CDEBUG(D_CONFIG, "%s: rsi upcall set to %s\n", rsicache->uc_name,
218                rsicache->uc_upcall);
219         rc = count;
220
221 out:
222         OBD_FREE(kbuf, count + 1);
223         return rc;
224 }
225 LPROC_SEQ_FOPS(rsi_upcall);
226
227 static ssize_t lprocfs_rsi_info_seq_write(struct file *file,
228                                           const char __user *buffer,
229                                           size_t count, void *data)
230 {
231         struct rsi_downcall_data *param;
232         int size = sizeof(*param), rc, checked = 0;
233
234 again:
235         if (count < size) {
236                 CERROR("%s: invalid data count = %lu, size = %d\n",
237                        rsicache->uc_name, (unsigned long)count, size);
238                 return -EINVAL;
239         }
240
241         OBD_ALLOC_LARGE(param, size);
242         if (param == NULL)
243                 return -ENOMEM;
244
245         if (copy_from_user(param, buffer, size)) {
246                 CERROR("%s: bad rsi data\n", rsicache->uc_name);
247                 GOTO(out, rc = -EFAULT);
248         }
249
250         if (checked == 0) {
251                 checked = 1;
252                 if (param->sid_magic != RSI_DOWNCALL_MAGIC) {
253                         CERROR("%s: rsi downcall bad params\n",
254                                rsicache->uc_name);
255                         GOTO(out, rc = -EINVAL);
256                 }
257
258                 rc = param->sid_len; /* save sid_len */
259                 OBD_FREE_LARGE(param, size);
260                 size = offsetof(struct rsi_downcall_data, sid_val[rc]);
261                 goto again;
262         }
263
264         rc = upcall_cache_downcall(rsicache, param->sid_err,
265                                    param->sid_hash, param);
266
267         /* The caller, i.e. the userspace process writing to rsi_info, only
268          * needs to know about invalid values. Other errors are processed
269          * directly in the kernel.
270          */
271         if (rc != -EINVAL)
272                 rc = 0;
273
274 out:
275         if (param != NULL)
276                 OBD_FREE_LARGE(param, size);
277
278         return rc ? rc : count;
279 }
280 LPROC_SEQ_FOPS_WR_ONLY(gss, rsi_info);
281
282 static int rsi_entry_expire_seq_show(struct seq_file *m,
283                                      void *data)
284 {
285         seq_printf(m, "%lld\n", rsicache->uc_entry_expire);
286         return 0;
287 }
288
289 static ssize_t rsi_entry_expire_seq_write(struct file *file,
290                                           const char __user *buffer,
291                                           size_t count, loff_t *off)
292 {
293         time64_t val;
294         int rc;
295
296         rc = kstrtoll_from_user(buffer, count, 10, &val);
297         if (rc)
298                 return rc;
299
300         if (val < 0)
301                 return -ERANGE;
302
303         rsicache->uc_entry_expire = val;
304
305         return count;
306 }
307 LPROC_SEQ_FOPS(rsi_entry_expire);
308
309 static int rsi_acquire_expire_seq_show(struct seq_file *m,
310                                        void *data)
311 {
312         seq_printf(m, "%lld\n", rsicache->uc_acquire_expire);
313         return 0;
314 }
315
316 static ssize_t rsi_acquire_expire_seq_write(struct file *file,
317                                             const char __user *buffer,
318                                             size_t count, loff_t *off)
319 {
320         time64_t val;
321         int rc;
322
323         rc = kstrtoll_from_user(buffer, count, 10, &val);
324         if (rc)
325                 return rc;
326
327         if (val < 0 || val > INT_MAX)
328                 return -ERANGE;
329
330         rsicache->uc_acquire_expire = val;
331
332         return count;
333 }
334 LPROC_SEQ_FOPS(rsi_acquire_expire);
335
336 static ssize_t lprocfs_rsc_info_seq_write(struct file *file,
337                                           const char __user *buffer,
338                                           size_t count, void *data)
339 {
340         struct rsc_downcall_data *param;
341         int size = sizeof(*param), rc, checked = 0;
342         struct gss_rsc rsc = { 0 }, *rscp = NULL;
343         char *mesg, *handle_buf;
344
345 again:
346         if (count < size) {
347                 CERROR("%s: invalid data count = %lu, size = %d\n",
348                        rsccache->uc_name, (unsigned long)count, size);
349                 return -EINVAL;
350         }
351
352         OBD_ALLOC_LARGE(param, size);
353         if (param == NULL)
354                 return -ENOMEM;
355
356         if (copy_from_user(param, buffer, size)) {
357                 CERROR("%s: bad rsc data\n", rsccache->uc_name);
358                 GOTO(out, rc = -EFAULT);
359         }
360
361         if (checked == 0) {
362                 checked = 1;
363                 if (param->scd_magic != RSC_DOWNCALL_MAGIC) {
364                         CERROR("%s: rsc downcall bad params\n",
365                                rsccache->uc_name);
366                         GOTO(out, rc = -EINVAL);
367                 }
368
369                 rc = param->scd_len; /* save scd_len */
370                 OBD_FREE_LARGE(param, size);
371                 size = offsetof(struct rsc_downcall_data, scd_val[rc]);
372                 goto again;
373         }
374
375         /* scd_val starts with handle.
376          * Use it to create cache entry.
377          */
378         mesg = param->scd_val;
379         gss_u32_read(&mesg, &rsc.sc_handle.len);
380         if (!rsc.sc_handle.len) {
381                 rc = -EINVAL;
382                 goto out;
383         }
384         OBD_ALLOC_LARGE(handle_buf, rsc.sc_handle.len);
385         if (!handle_buf) {
386                 rc = -ENOMEM;
387                 goto out;
388         }
389         memset(handle_buf, 0, rsc.sc_handle.len);
390         mesg = param->scd_val;
391         rc = gss_buffer_read(&mesg, handle_buf, rsc.sc_handle.len);
392         if (rc < 0) {
393                 OBD_FREE_LARGE(handle_buf, rsc.sc_handle.len);
394                 rc = -EINVAL;
395                 goto out;
396         }
397         rsc.sc_handle.data = handle_buf;
398
399         /* create cache entry on-the-fly */
400         rscp = rsc_entry_get(rsccache, &rsc);
401         __rsc_free(&rsc);
402
403         if (IS_ERR_OR_NULL(rscp)) {
404                 if (IS_ERR(rscp))
405                         rc = PTR_ERR(rscp);
406                 else
407                         rc = -EINVAL;
408                 CERROR("%s: error in rsc_entry_get: rc = %d\n",
409                        param->scd_mechname, rc);
410                 goto out;
411         }
412
413         /* now that entry has been created, downcall can be done,
414          * but we have to tell acquiring is in progress
415          */
416         upcall_cache_update_entry(rsccache, rscp->sc_uc_entry,
417                                   0, UC_CACHE_ACQUIRING);
418         rc = upcall_cache_downcall(rsccache, param->scd_err,
419                                    rscp->sc_uc_entry->ue_key, param);
420
421 out:
422         if (!IS_ERR_OR_NULL(rscp))
423                 rsc_entry_put(rsccache, rscp);
424         if (param)
425                 OBD_FREE_LARGE(param, size);
426
427         return rc ? rc : count;
428 }
429 LPROC_SEQ_FOPS_WR_ONLY(gss, rsc_info);
430
431 static struct ldebugfs_vars gss_debugfs_vars[] = {
432         { .name =       "replays",
433           .fops =       &gss_proc_oos_fops      },
434         { .name =       "init_channel",
435           .fops =       &gss_proc_secinit,
436           .proc_mode =  0222                    },
437         { NULL }
438 };
439
440 static struct lprocfs_vars gss_lprocfs_vars[] = {
441         { .name =       "krb5_allow_old_client_csum",
442           .fops =       &sptlrpc_krb5_allow_old_client_csum_fops },
443 #ifdef HAVE_GSS_KEYRING
444         { .name =       "gss_check_upcall_ns",
445           .fops =       &sptlrpc_gss_check_upcall_ns_fops },
446 #endif
447         { .name =       "rsi_upcall",
448           .fops =       &rsi_upcall_fops },
449         { .name =       "rsi_info",
450           .fops =       &gss_rsi_info_fops },
451         { .name =       "rsi_entry_expire",
452           .fops =       &rsi_entry_expire_fops },
453         { .name =       "rsi_acquire_expire",
454           .fops =       &rsi_acquire_expire_fops },
455         { .name =       "rsc_info",
456           .fops =       &gss_rsc_info_fops },
457         { NULL }
458 };
459
460 /*
461  * for userspace helper lgss_keyring.
462  *
463  * debug_level: [0, 4], defined in utils/gss/lgss_utils.h
464  */
465 static int gss_lk_debug_level = 1;
466
467 static int gss_lk_proc_dl_seq_show(struct seq_file *m, void *v)
468 {
469         seq_printf(m, "%u\n", gss_lk_debug_level);
470         return 0;
471 }
472
473 static ssize_t
474 gss_lk_proc_dl_seq_write(struct file *file, const char __user *buffer,
475                                 size_t count, loff_t *off)
476 {
477         unsigned int val;
478         int rc;
479
480         rc = kstrtouint_from_user(buffer, count, 0, &val);
481         if (rc < 0)
482                 return rc;
483
484         if (val > 4)
485                 return -ERANGE;
486
487         gss_lk_debug_level = val;
488
489         return count;
490 }
491 LDEBUGFS_SEQ_FOPS(gss_lk_proc_dl);
492
493 static struct ldebugfs_vars gss_lk_debugfs_vars[] = {
494         { .name =       "debug_level",
495           .fops =       &gss_lk_proc_dl_fops    },
496         { NULL }
497 };
498
499 void gss_exit_tunables(void)
500 {
501         debugfs_remove_recursive(gss_debugfs_dir_lk);
502         gss_debugfs_dir_lk = NULL;
503
504         debugfs_remove_recursive(gss_debugfs_dir);
505         gss_debugfs_dir = NULL;
506
507         if (!IS_ERR_OR_NULL(gss_lprocfs_dir))
508                 lprocfs_remove(&gss_lprocfs_dir);
509 }
510
511 int gss_init_tunables(void)
512 {
513         int     rc;
514
515         spin_lock_init(&gss_stat_oos.oos_lock);
516
517         gss_debugfs_dir = debugfs_create_dir("gss", sptlrpc_debugfs_dir);
518         ldebugfs_add_vars(gss_debugfs_dir, gss_debugfs_vars, NULL);
519
520         gss_debugfs_dir_lk = debugfs_create_dir("lgss_keyring",
521                                                 gss_debugfs_dir);
522         ldebugfs_add_vars(gss_debugfs_dir_lk, gss_lk_debugfs_vars, NULL);
523
524         gss_lprocfs_dir = lprocfs_register("gss", sptlrpc_lprocfs_dir,
525                                            gss_lprocfs_vars, NULL);
526         if (IS_ERR_OR_NULL(gss_lprocfs_dir)) {
527                 rc = gss_lprocfs_dir ? PTR_ERR(gss_lprocfs_dir) : -ENOMEM;
528                 gss_lprocfs_dir = NULL;
529                 GOTO(out, rc);
530         }
531
532         return 0;
533
534 out:
535         CERROR("failed to initialize gss lproc entries: %d\n", rc);
536         gss_exit_tunables();
537         return rc;
538 }