Whamcloud - gitweb
LU-17000 ptlrpc: fix string overflow warnings
[fs/lustre-release.git] / lustre / ptlrpc / sec_lproc.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) 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ptlrpc/sec_lproc.c
32  *
33  * Author: Eric Mei <ericm@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_SEC
37
38 #include <libcfs/libcfs.h>
39 #include <linux/crypto.h>
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
45 #include <lustre_import.h>
46 #include <lustre_dlm.h>
47 #include <lustre_sec.h>
48
49 #include "ptlrpc_internal.h"
50
51 static char *sec_flags2str(unsigned long flags, char *buf, int bufsize)
52 {
53         buf[0] = '\0';
54
55         if (flags & PTLRPC_SEC_FL_REVERSE)
56                 strlcat(buf, "reverse,", bufsize);
57         if (flags & PTLRPC_SEC_FL_ROOTONLY)
58                 strlcat(buf, "rootonly,", bufsize);
59         if (flags & PTLRPC_SEC_FL_UDESC)
60                 strlcat(buf, "udesc,", bufsize);
61         if (flags & PTLRPC_SEC_FL_BULK)
62                 strlcat(buf, "bulk,", bufsize);
63         if (buf[0] == '\0')
64                 strlcat(buf, "-,", bufsize);
65
66         return buf;
67 }
68
69 static int sptlrpc_info_lprocfs_seq_show(struct seq_file *seq, void *v)
70 {
71         struct obd_device *obd = seq->private;
72         struct client_obd *cli = &obd->u.cli;
73         struct ptlrpc_sec *sec = NULL;
74         char               str[32];
75
76         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
77                 strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
78                 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) == 0 ||
79                 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0 ||
80                 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0);
81
82         if (cli->cl_import)
83                 sec = sptlrpc_import_sec_ref(cli->cl_import);
84         if (sec == NULL)
85                 goto out;
86
87         sec_flags2str(sec->ps_flvr.sf_flags, str, sizeof(str));
88
89         seq_printf(seq, "rpc flavor:    %s\n",
90                    sptlrpc_flavor2name_base(sec->ps_flvr.sf_rpc));
91         seq_printf(seq, "bulk flavor:   %s\n",
92                    sptlrpc_flavor2name_bulk(&sec->ps_flvr, str, sizeof(str)));
93         seq_printf(seq, "flags:         %s\n",
94                    sec_flags2str(sec->ps_flvr.sf_flags, str, sizeof(str)));
95         seq_printf(seq, "id:            %d\n", sec->ps_id);
96         seq_printf(seq, "refcount:      %d\n",
97                    atomic_read(&sec->ps_refcount));
98         seq_printf(seq, "nctx:  %d\n", atomic_read(&sec->ps_nctx));
99         seq_printf(seq, "gc internal    %lld\n", sec->ps_gc_interval);
100         seq_printf(seq, "gc next        %lld\n",
101                    sec->ps_gc_interval ?
102                    (s64)(sec->ps_gc_next - ktime_get_real_seconds()) : 0ll);
103
104         sptlrpc_sec_put(sec);
105 out:
106         return 0;
107 }
108
109 LDEBUGFS_SEQ_FOPS_RO(sptlrpc_info_lprocfs);
110
111 static int sptlrpc_ctxs_lprocfs_seq_show(struct seq_file *seq, void *v)
112 {
113         struct obd_device *obd = seq->private;
114         struct client_obd *cli = &obd->u.cli;
115         struct ptlrpc_sec *sec = NULL;
116
117         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
118                 strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
119                 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) == 0 ||
120                 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0 ||
121                 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0);
122
123         if (cli->cl_import)
124                 sec = sptlrpc_import_sec_ref(cli->cl_import);
125         if (sec == NULL)
126                 goto out;
127
128         if (sec->ps_policy->sp_cops->display)
129                 sec->ps_policy->sp_cops->display(sec, seq);
130
131         sptlrpc_sec_put(sec);
132 out:
133         return 0;
134 }
135
136 LDEBUGFS_SEQ_FOPS_RO(sptlrpc_ctxs_lprocfs);
137
138 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
139 static ssize_t sepol_seq_write_old(struct obd_device *obd,
140                                    const char __user *buffer, size_t count)
141 {
142         struct client_obd *cli = &obd->u.cli;
143         struct obd_import *imp = cli->cl_import;
144         struct sepol_downcall_data_old *param;
145         size_t maxlen = sizeof(imp->imp_sec->ps_sepol);
146         size_t size = sizeof(*param);
147         size_t maxparam = sizeof(*param) + maxlen;
148         int len;
149         int rc = 0;
150
151         if (count <= size) {
152                 rc = -EINVAL;
153                 CERROR("%s: invalid data count %zu <= size %zu: rc = %d\n",
154                        obd->obd_name, count, size, rc);
155                 return rc;
156         }
157
158         OBD_ALLOC(param, maxparam);
159         if (!param)
160                 return -ENOMEM;
161
162         if (copy_from_user(param, buffer, min(count, maxparam))) {
163                 rc = -EFAULT;
164                 CERROR("%s: bad sepol data: rc = %d\n", obd->obd_name, rc);
165                 GOTO(out, rc);
166         }
167
168         if (param->sdd_magic != SEPOL_DOWNCALL_MAGIC_OLD) {
169                 rc = -EINVAL;
170                 CERROR("%s: sepol downcall bad magic %#08x != %#08x: rc = %d\n",
171                        obd->obd_name, param->sdd_magic,
172                        SEPOL_DOWNCALL_MAGIC_OLD, rc);
173                 GOTO(out, rc);
174         }
175
176         len = param->sdd_sepol_len;
177         if (len == 0 || len >= maxlen) {
178                 rc = -EINVAL;
179                 CERROR("%s: bad sepol len %u >= maxlen %zu: rc = %d\n",
180                        obd->obd_name, len, maxlen, rc);
181                 GOTO(out, rc);
182         }
183         size = offsetof(typeof(*param), sdd_sepol[len]);
184
185         if (count < size) {
186                 rc = -EINVAL;
187                 CERROR("%s: bad sepol count %zu < total size %zu: rc = %d\n",
188                        obd->obd_name, count, size, rc);
189                 GOTO(out, rc);
190         }
191
192         spin_lock(&imp->imp_sec->ps_lock);
193         memcpy(imp->imp_sec->ps_sepol, param->sdd_sepol, len);
194         imp->imp_sec->ps_sepol[len + 1] = '\0';
195         imp->imp_sec->ps_sepol_mtime = ktime_set(param->sdd_sepol_mtime, 0);
196         spin_unlock(&imp->imp_sec->ps_lock);
197
198 out:
199         OBD_FREE(param, maxparam);
200
201         return rc ?: count;
202 }
203 #endif
204
205 static ssize_t
206 ldebugfs_sptlrpc_sepol_seq_write(struct file *file, const char __user *buffer,
207                                  size_t count, void *data)
208 {
209         struct seq_file *seq = file->private_data;
210         struct obd_device *obd = seq->private;
211         struct client_obd *cli = &obd->u.cli;
212         struct obd_import *imp = cli->cl_import;
213         struct sepol_downcall_data *param;
214         size_t maxlen = sizeof(imp->imp_sec->ps_sepol);
215         size_t size = sizeof(*param);
216         size_t maxparam = size + maxlen;
217         int len;
218         int rc = 0;
219
220         if (count <= size) {
221                 rc = -EINVAL;
222                 CERROR("%s: invalid data count %zu <= size %zu: rc = %d\n",
223                        obd->obd_name, count, size, rc);
224                 return rc;
225         }
226
227 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
228         {
229                 __u32 magic;
230
231                 if (copy_from_user(&magic, buffer, sizeof(magic))) {
232                         rc = -EFAULT;
233                         CERROR("%s: bad sepol magic data: rc = %d\n",
234                                obd->obd_name, rc);
235                         return rc;
236                 }
237
238                 if (unlikely(magic == SEPOL_DOWNCALL_MAGIC_OLD))
239                         return sepol_seq_write_old(obd, buffer, count);
240         }
241 #endif
242
243         OBD_ALLOC(param, maxparam);
244         if (!param)
245                 return -ENOMEM;
246
247         if (copy_from_user(param, buffer, min(count, maxparam))) {
248                 rc = -EFAULT;
249                 CERROR("%s: bad sepol data: rc = %d\n", obd->obd_name, rc);
250                 GOTO(out, rc);
251         }
252
253         if (param->sdd_magic != SEPOL_DOWNCALL_MAGIC) {
254                 rc = -EINVAL;
255                 CERROR("%s: sepol downcall bad magic %#08x != %#08x: rc = %d\n",
256                        obd->obd_name, param->sdd_magic,
257                        SEPOL_DOWNCALL_MAGIC, rc);
258                 GOTO(out, rc);
259         }
260
261         len = param->sdd_sepol_len;
262         if (len == 0 || len >= maxlen) {
263                 rc = -EINVAL;
264                 CERROR("%s: bad sepol len %u >= maxlen %zu: rc = %d\n",
265                        obd->obd_name, len, maxlen, rc);
266                 GOTO(out, rc);
267         }
268         size = offsetof(typeof(*param), sdd_sepol[len]);
269
270         if (count < size) {
271                 rc = -EINVAL;
272                 CERROR("%s: bad sepol count %zu < total size %zu: rc = %d\n",
273                        obd->obd_name, count, size, rc);
274                 GOTO(out, rc);
275         }
276
277         spin_lock(&imp->imp_sec->ps_lock);
278         memcpy(imp->imp_sec->ps_sepol, param->sdd_sepol, len);
279         imp->imp_sec->ps_sepol[len + 1] = '\0';
280         imp->imp_sec->ps_sepol_mtime = ktime_set(param->sdd_sepol_mtime, 0);
281         spin_unlock(&imp->imp_sec->ps_lock);
282
283 out:
284         OBD_FREE(param, maxparam);
285
286         return rc ?: count;
287 }
288 LDEBUGFS_FOPS_WR_ONLY(srpc, sptlrpc_sepol);
289
290 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *obd)
291 {
292         if (strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) != 0 &&
293             strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) != 0 &&
294             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) != 0 &&
295             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) != 0 &&
296             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) != 0) {
297                 CERROR("can't register lproc for obd type %s\n",
298                        obd->obd_type->typ_name);
299                 return -EINVAL;
300         }
301
302         debugfs_create_file("srpc_info", 0444, obd->obd_debugfs_entry, obd,
303                             &sptlrpc_info_lprocfs_fops);
304
305         debugfs_create_file("srpc_contexts", 0444, obd->obd_debugfs_entry, obd,
306                             &sptlrpc_ctxs_lprocfs_fops);
307
308         debugfs_create_file("srpc_sepol", 0200, obd->obd_debugfs_entry, obd,
309                             &srpc_sptlrpc_sepol_fops);
310
311         return 0;
312 }
313 EXPORT_SYMBOL(sptlrpc_lprocfs_cliobd_attach);
314
315 LDEBUGFS_SEQ_FOPS_RO(sptlrpc_proc_enc_pool);
316
317 static struct ldebugfs_vars sptlrpc_lprocfs_vars[] = {
318         { .name =       "encrypt_page_pools",
319           .fops =       &sptlrpc_proc_enc_pool_fops     },
320         { NULL }
321 };
322
323 struct dentry *sptlrpc_debugfs_dir;
324 EXPORT_SYMBOL(sptlrpc_debugfs_dir);
325
326 struct proc_dir_entry *sptlrpc_lprocfs_dir;
327 EXPORT_SYMBOL(sptlrpc_lprocfs_dir);
328
329 int sptlrpc_lproc_init(void)
330 {
331         int rc = 0;
332
333         LASSERT(sptlrpc_debugfs_dir == NULL);
334
335         sptlrpc_debugfs_dir = debugfs_create_dir("sptlrpc",
336                                                  debugfs_lustre_root);
337         ldebugfs_add_vars(sptlrpc_debugfs_dir, sptlrpc_lprocfs_vars, NULL);
338
339         sptlrpc_lprocfs_dir = lprocfs_register("sptlrpc", proc_lustre_root,
340                                                NULL, NULL);
341         if (IS_ERR_OR_NULL(sptlrpc_lprocfs_dir)) {
342                 rc = PTR_ERR(sptlrpc_lprocfs_dir);
343                 rc = sptlrpc_lprocfs_dir ? PTR_ERR(sptlrpc_lprocfs_dir)
344                         : -ENOMEM;
345                 sptlrpc_lprocfs_dir = NULL;
346         }
347         return rc;
348 }
349
350 void sptlrpc_lproc_fini(void)
351 {
352         debugfs_remove_recursive(sptlrpc_debugfs_dir);
353         sptlrpc_debugfs_dir = NULL;
354
355         if (!IS_ERR_OR_NULL(sptlrpc_lprocfs_dir))
356                 lprocfs_remove(&sptlrpc_lprocfs_dir);
357 }