Whamcloud - gitweb
LU-17484 gss: reply error for SEC_CTX_INIT on wrong node
[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         if (count >= UC_CACHE_UPCALL_MAXPATH) {
202                 CERROR("%s: rsi upcall too long\n", rsicache->uc_name);
203                 return -EINVAL;
204         }
205
206         OBD_ALLOC(kbuf, count + 1);
207         if (kbuf == NULL)
208                 return -ENOMEM;
209
210         if (copy_from_user(kbuf, buffer, count))
211                 GOTO(out, rc = -EFAULT);
212
213         kbuf[count] = '\0';
214
215         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
216         down_write(&rsicache->uc_upcall_rwsem);
217         rc = sscanf(kbuf, "%s", rsicache->uc_upcall);
218         up_write(&rsicache->uc_upcall_rwsem);
219
220         if (rc != 1) {
221                 CERROR("%s: invalid rsi upcall provided\n", rsicache->uc_name);
222                 GOTO(out, rc = -EINVAL);
223         }
224
225         CDEBUG(D_CONFIG, "%s: rsi upcall set to %s\n", rsicache->uc_name,
226                rsicache->uc_upcall);
227
228 out:
229         OBD_FREE(kbuf, count + 1);
230         return count;
231 }
232 LPROC_SEQ_FOPS(rsi_upcall);
233
234 static ssize_t lprocfs_rsi_info_seq_write(struct file *file,
235                                           const char __user *buffer,
236                                           size_t count, void *data)
237 {
238         struct rsi_downcall_data *param;
239         int size = sizeof(*param), rc, checked = 0;
240
241 again:
242         if (count < size) {
243                 CERROR("%s: invalid data count = %lu, size = %d\n",
244                        rsicache->uc_name, (unsigned long)count, size);
245                 return -EINVAL;
246         }
247
248         OBD_ALLOC_LARGE(param, size);
249         if (param == NULL)
250                 return -ENOMEM;
251
252         if (copy_from_user(param, buffer, size)) {
253                 CERROR("%s: bad rsi data\n", rsicache->uc_name);
254                 GOTO(out, rc = -EFAULT);
255         }
256
257         if (checked == 0) {
258                 checked = 1;
259                 if (param->sid_magic != RSI_DOWNCALL_MAGIC) {
260                         CERROR("%s: rsi downcall bad params\n",
261                                rsicache->uc_name);
262                         GOTO(out, rc = -EINVAL);
263                 }
264
265                 rc = param->sid_len; /* save sid_len */
266                 OBD_FREE_LARGE(param, size);
267                 size = offsetof(struct rsi_downcall_data, sid_val[rc]);
268                 goto again;
269         }
270
271         rc = upcall_cache_downcall(rsicache, param->sid_err,
272                                    param->sid_hash, param);
273
274         /* The caller, i.e. the userspace process writing to rsi_info, only
275          * needs to know about invalid values. Other errors are processed
276          * directly in the kernel.
277          */
278         if (rc != -EINVAL)
279                 rc = 0;
280
281 out:
282         if (param != NULL)
283                 OBD_FREE_LARGE(param, size);
284
285         return rc ? rc : count;
286 }
287 LPROC_SEQ_FOPS_WR_ONLY(gss, rsi_info);
288
289 static int rsi_entry_expire_seq_show(struct seq_file *m,
290                                      void *data)
291 {
292         seq_printf(m, "%lld\n", rsicache->uc_entry_expire);
293         return 0;
294 }
295
296 static ssize_t rsi_entry_expire_seq_write(struct file *file,
297                                           const char __user *buffer,
298                                           size_t count, loff_t *off)
299 {
300         time64_t val;
301         int rc;
302
303         rc = kstrtoll_from_user(buffer, count, 10, &val);
304         if (rc)
305                 return rc;
306
307         if (val < 0)
308                 return -ERANGE;
309
310         rsicache->uc_entry_expire = val;
311
312         return count;
313 }
314 LPROC_SEQ_FOPS(rsi_entry_expire);
315
316 static int rsi_acquire_expire_seq_show(struct seq_file *m,
317                                        void *data)
318 {
319         seq_printf(m, "%lld\n", rsicache->uc_acquire_expire);
320         return 0;
321 }
322
323 static ssize_t rsi_acquire_expire_seq_write(struct file *file,
324                                             const char __user *buffer,
325                                             size_t count, loff_t *off)
326 {
327         time64_t val;
328         int rc;
329
330         rc = kstrtoll_from_user(buffer, count, 10, &val);
331         if (rc)
332                 return rc;
333
334         if (val < 0 || val > INT_MAX)
335                 return -ERANGE;
336
337         rsicache->uc_acquire_expire = val;
338
339         return count;
340 }
341 LPROC_SEQ_FOPS(rsi_acquire_expire);
342
343 static ssize_t lprocfs_rsc_info_seq_write(struct file *file,
344                                           const char __user *buffer,
345                                           size_t count, void *data)
346 {
347         struct rsc_downcall_data *param;
348         int size = sizeof(*param), rc, checked = 0;
349         struct gss_rsc rsc = { 0 }, *rscp = NULL;
350         char *mesg, *handle_buf;
351
352 again:
353         if (count < size) {
354                 CERROR("%s: invalid data count = %lu, size = %d\n",
355                        rsccache->uc_name, (unsigned long)count, size);
356                 return -EINVAL;
357         }
358
359         OBD_ALLOC_LARGE(param, size);
360         if (param == NULL)
361                 return -ENOMEM;
362
363         if (copy_from_user(param, buffer, size)) {
364                 CERROR("%s: bad rsc data\n", rsccache->uc_name);
365                 GOTO(out, rc = -EFAULT);
366         }
367
368         if (checked == 0) {
369                 checked = 1;
370                 if (param->scd_magic != RSC_DOWNCALL_MAGIC) {
371                         CERROR("%s: rsc downcall bad params\n",
372                                rsccache->uc_name);
373                         GOTO(out, rc = -EINVAL);
374                 }
375
376                 rc = param->scd_len; /* save scd_len */
377                 OBD_FREE_LARGE(param, size);
378                 size = offsetof(struct rsc_downcall_data, scd_val[rc]);
379                 goto again;
380         }
381
382         /* scd_val starts with handle.
383          * Use it to create cache entry.
384          */
385         mesg = param->scd_val;
386         gss_u32_read(&mesg, &rsc.sc_handle.len);
387         if (!rsc.sc_handle.len) {
388                 rc = -EINVAL;
389                 goto out;
390         }
391         OBD_ALLOC_LARGE(handle_buf, rsc.sc_handle.len);
392         if (!handle_buf) {
393                 rc = -ENOMEM;
394                 goto out;
395         }
396         memset(handle_buf, 0, rsc.sc_handle.len);
397         mesg = param->scd_val;
398         rc = gss_buffer_read(&mesg, handle_buf, rsc.sc_handle.len);
399         if (rc < 0) {
400                 OBD_FREE_LARGE(handle_buf, rsc.sc_handle.len);
401                 rc = -EINVAL;
402                 goto out;
403         }
404         rsc.sc_handle.data = handle_buf;
405
406         /* create cache entry on-the-fly */
407         rscp = rsc_entry_get(rsccache, &rsc);
408         __rsc_free(&rsc);
409
410         if (IS_ERR_OR_NULL(rscp)) {
411                 if (IS_ERR(rscp))
412                         rc = PTR_ERR(rscp);
413                 else
414                         rc = -EINVAL;
415                 CERROR("%s: error in rsc_entry_get: rc = %d\n",
416                        param->scd_mechname, rc);
417                 goto out;
418         }
419
420         /* now that entry has been created, downcall can be done,
421          * but we have to tell acquiring is in progress
422          */
423         upcall_cache_update_entry(rsccache, rscp->sc_uc_entry,
424                                   0, UC_CACHE_ACQUIRING);
425         rc = upcall_cache_downcall(rsccache, param->scd_err,
426                                    rscp->sc_uc_entry->ue_key, param);
427
428 out:
429         if (!IS_ERR_OR_NULL(rscp))
430                 rsc_entry_put(rsccache, rscp);
431         if (param)
432                 OBD_FREE_LARGE(param, size);
433
434         return rc ? rc : count;
435 }
436 LPROC_SEQ_FOPS_WR_ONLY(gss, rsc_info);
437
438 static struct ldebugfs_vars gss_debugfs_vars[] = {
439         { .name =       "replays",
440           .fops =       &gss_proc_oos_fops      },
441         { .name =       "init_channel",
442           .fops =       &gss_proc_secinit,
443           .proc_mode =  0222                    },
444         { NULL }
445 };
446
447 static struct lprocfs_vars gss_lprocfs_vars[] = {
448         { .name =       "krb5_allow_old_client_csum",
449           .fops =       &sptlrpc_krb5_allow_old_client_csum_fops },
450 #ifdef HAVE_GSS_KEYRING
451         { .name =       "gss_check_upcall_ns",
452           .fops =       &sptlrpc_gss_check_upcall_ns_fops },
453 #endif
454         { .name =       "rsi_upcall",
455           .fops =       &rsi_upcall_fops },
456         { .name =       "rsi_info",
457           .fops =       &gss_rsi_info_fops },
458         { .name =       "rsi_entry_expire",
459           .fops =       &rsi_entry_expire_fops },
460         { .name =       "rsi_acquire_expire",
461           .fops =       &rsi_acquire_expire_fops },
462         { .name =       "rsc_info",
463           .fops =       &gss_rsc_info_fops },
464         { NULL }
465 };
466
467 /*
468  * for userspace helper lgss_keyring.
469  *
470  * debug_level: [0, 4], defined in utils/gss/lgss_utils.h
471  */
472 static int gss_lk_debug_level = 1;
473
474 static int gss_lk_proc_dl_seq_show(struct seq_file *m, void *v)
475 {
476         seq_printf(m, "%u\n", gss_lk_debug_level);
477         return 0;
478 }
479
480 static ssize_t
481 gss_lk_proc_dl_seq_write(struct file *file, const char __user *buffer,
482                                 size_t count, loff_t *off)
483 {
484         unsigned int val;
485         int rc;
486
487         rc = kstrtouint_from_user(buffer, count, 0, &val);
488         if (rc < 0)
489                 return rc;
490
491         if (val > 4)
492                 return -ERANGE;
493
494         gss_lk_debug_level = val;
495
496         return count;
497 }
498 LDEBUGFS_SEQ_FOPS(gss_lk_proc_dl);
499
500 static struct ldebugfs_vars gss_lk_debugfs_vars[] = {
501         { .name =       "debug_level",
502           .fops =       &gss_lk_proc_dl_fops    },
503         { NULL }
504 };
505
506 void gss_exit_tunables(void)
507 {
508         debugfs_remove_recursive(gss_debugfs_dir_lk);
509         gss_debugfs_dir_lk = NULL;
510
511         debugfs_remove_recursive(gss_debugfs_dir);
512         gss_debugfs_dir = NULL;
513
514         if (!IS_ERR_OR_NULL(gss_lprocfs_dir))
515                 lprocfs_remove(&gss_lprocfs_dir);
516 }
517
518 int gss_init_tunables(void)
519 {
520         int     rc;
521
522         spin_lock_init(&gss_stat_oos.oos_lock);
523
524         gss_debugfs_dir = debugfs_create_dir("gss", sptlrpc_debugfs_dir);
525         ldebugfs_add_vars(gss_debugfs_dir, gss_debugfs_vars, NULL);
526
527         gss_debugfs_dir_lk = debugfs_create_dir("lgss_keyring",
528                                                 gss_debugfs_dir);
529         ldebugfs_add_vars(gss_debugfs_dir_lk, gss_lk_debugfs_vars, NULL);
530
531         gss_lprocfs_dir = lprocfs_register("gss", sptlrpc_lprocfs_dir,
532                                            gss_lprocfs_vars, NULL);
533         if (IS_ERR_OR_NULL(gss_lprocfs_dir)) {
534                 rc = gss_lprocfs_dir ? PTR_ERR(gss_lprocfs_dir) : -ENOMEM;
535                 gss_lprocfs_dir = NULL;
536                 GOTO(out, rc);
537         }
538
539         return 0;
540
541 out:
542         CERROR("failed to initialize gss lproc entries: %d\n", rc);
543         gss_exit_tunables();
544         return rc;
545 }