Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux...
[linux-block.git] / drivers / infiniband / core / security.c
1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #ifdef CONFIG_SECURITY_INFINIBAND
34
35 #include <linux/security.h>
36 #include <linux/completion.h>
37 #include <linux/list.h>
38
39 #include <rdma/ib_verbs.h>
40 #include <rdma/ib_cache.h>
41 #include "core_priv.h"
42 #include "mad_priv.h"
43
44 static struct pkey_index_qp_list *get_pkey_idx_qp_list(struct ib_port_pkey *pp)
45 {
46         struct pkey_index_qp_list *pkey = NULL;
47         struct pkey_index_qp_list *tmp_pkey;
48         struct ib_device *dev = pp->sec->dev;
49
50         spin_lock(&dev->port_pkey_list[pp->port_num].list_lock);
51         list_for_each_entry(tmp_pkey,
52                             &dev->port_pkey_list[pp->port_num].pkey_list,
53                             pkey_index_list) {
54                 if (tmp_pkey->pkey_index == pp->pkey_index) {
55                         pkey = tmp_pkey;
56                         break;
57                 }
58         }
59         spin_unlock(&dev->port_pkey_list[pp->port_num].list_lock);
60         return pkey;
61 }
62
63 static int get_pkey_and_subnet_prefix(struct ib_port_pkey *pp,
64                                       u16 *pkey,
65                                       u64 *subnet_prefix)
66 {
67         struct ib_device *dev = pp->sec->dev;
68         int ret;
69
70         ret = ib_get_cached_pkey(dev, pp->port_num, pp->pkey_index, pkey);
71         if (ret)
72                 return ret;
73
74         ret = ib_get_cached_subnet_prefix(dev, pp->port_num, subnet_prefix);
75
76         return ret;
77 }
78
79 static int enforce_qp_pkey_security(u16 pkey,
80                                     u64 subnet_prefix,
81                                     struct ib_qp_security *qp_sec)
82 {
83         struct ib_qp_security *shared_qp_sec;
84         int ret;
85
86         ret = security_ib_pkey_access(qp_sec->security, subnet_prefix, pkey);
87         if (ret)
88                 return ret;
89
90         if (qp_sec->qp == qp_sec->qp->real_qp) {
91                 list_for_each_entry(shared_qp_sec,
92                                     &qp_sec->shared_qp_list,
93                                     shared_qp_list) {
94                         ret = security_ib_pkey_access(shared_qp_sec->security,
95                                                       subnet_prefix,
96                                                       pkey);
97                         if (ret)
98                                 return ret;
99                 }
100         }
101         return 0;
102 }
103
104 /* The caller of this function must hold the QP security
105  * mutex of the QP of the security structure in *pps.
106  *
107  * It takes separate ports_pkeys and security structure
108  * because in some cases the pps will be for a new settings
109  * or the pps will be for the real QP and security structure
110  * will be for a shared QP.
111  */
112 static int check_qp_port_pkey_settings(struct ib_ports_pkeys *pps,
113                                        struct ib_qp_security *sec)
114 {
115         u64 subnet_prefix;
116         u16 pkey;
117         int ret = 0;
118
119         if (!pps)
120                 return 0;
121
122         if (pps->main.state != IB_PORT_PKEY_NOT_VALID) {
123                 get_pkey_and_subnet_prefix(&pps->main,
124                                            &pkey,
125                                            &subnet_prefix);
126
127                 ret = enforce_qp_pkey_security(pkey,
128                                                subnet_prefix,
129                                                sec);
130         }
131         if (ret)
132                 return ret;
133
134         if (pps->alt.state != IB_PORT_PKEY_NOT_VALID) {
135                 get_pkey_and_subnet_prefix(&pps->alt,
136                                            &pkey,
137                                            &subnet_prefix);
138
139                 ret = enforce_qp_pkey_security(pkey,
140                                                subnet_prefix,
141                                                sec);
142         }
143
144         return ret;
145 }
146
147 /* The caller of this function must hold the QP security
148  * mutex.
149  */
150 static void qp_to_error(struct ib_qp_security *sec)
151 {
152         struct ib_qp_security *shared_qp_sec;
153         struct ib_qp_attr attr = {
154                 .qp_state = IB_QPS_ERR
155         };
156         struct ib_event event = {
157                 .event = IB_EVENT_QP_FATAL
158         };
159
160         /* If the QP is in the process of being destroyed
161          * the qp pointer in the security structure is
162          * undefined.  It cannot be modified now.
163          */
164         if (sec->destroying)
165                 return;
166
167         ib_modify_qp(sec->qp,
168                      &attr,
169                      IB_QP_STATE);
170
171         if (sec->qp->event_handler && sec->qp->qp_context) {
172                 event.element.qp = sec->qp;
173                 sec->qp->event_handler(&event,
174                                        sec->qp->qp_context);
175         }
176
177         list_for_each_entry(shared_qp_sec,
178                             &sec->shared_qp_list,
179                             shared_qp_list) {
180                 struct ib_qp *qp = shared_qp_sec->qp;
181
182                 if (qp->event_handler && qp->qp_context) {
183                         event.element.qp = qp;
184                         event.device = qp->device;
185                         qp->event_handler(&event,
186                                           qp->qp_context);
187                 }
188         }
189 }
190
191 static inline void check_pkey_qps(struct pkey_index_qp_list *pkey,
192                                   struct ib_device *device,
193                                   u8 port_num,
194                                   u64 subnet_prefix)
195 {
196         struct ib_port_pkey *pp, *tmp_pp;
197         bool comp;
198         LIST_HEAD(to_error_list);
199         u16 pkey_val;
200
201         if (!ib_get_cached_pkey(device,
202                                 port_num,
203                                 pkey->pkey_index,
204                                 &pkey_val)) {
205                 spin_lock(&pkey->qp_list_lock);
206                 list_for_each_entry(pp, &pkey->qp_list, qp_list) {
207                         if (atomic_read(&pp->sec->error_list_count))
208                                 continue;
209
210                         if (enforce_qp_pkey_security(pkey_val,
211                                                      subnet_prefix,
212                                                      pp->sec)) {
213                                 atomic_inc(&pp->sec->error_list_count);
214                                 list_add(&pp->to_error_list,
215                                          &to_error_list);
216                         }
217                 }
218                 spin_unlock(&pkey->qp_list_lock);
219         }
220
221         list_for_each_entry_safe(pp,
222                                  tmp_pp,
223                                  &to_error_list,
224                                  to_error_list) {
225                 mutex_lock(&pp->sec->mutex);
226                 qp_to_error(pp->sec);
227                 list_del(&pp->to_error_list);
228                 atomic_dec(&pp->sec->error_list_count);
229                 comp = pp->sec->destroying;
230                 mutex_unlock(&pp->sec->mutex);
231
232                 if (comp)
233                         complete(&pp->sec->error_complete);
234         }
235 }
236
237 /* The caller of this function must hold the QP security
238  * mutex.
239  */
240 static int port_pkey_list_insert(struct ib_port_pkey *pp)
241 {
242         struct pkey_index_qp_list *tmp_pkey;
243         struct pkey_index_qp_list *pkey;
244         struct ib_device *dev;
245         u8 port_num = pp->port_num;
246         int ret = 0;
247
248         if (pp->state != IB_PORT_PKEY_VALID)
249                 return 0;
250
251         dev = pp->sec->dev;
252
253         pkey = get_pkey_idx_qp_list(pp);
254
255         if (!pkey) {
256                 bool found = false;
257
258                 pkey = kzalloc(sizeof(*pkey), GFP_KERNEL);
259                 if (!pkey)
260                         return -ENOMEM;
261
262                 spin_lock(&dev->port_pkey_list[port_num].list_lock);
263                 /* Check for the PKey again.  A racing process may
264                  * have created it.
265                  */
266                 list_for_each_entry(tmp_pkey,
267                                     &dev->port_pkey_list[port_num].pkey_list,
268                                     pkey_index_list) {
269                         if (tmp_pkey->pkey_index == pp->pkey_index) {
270                                 kfree(pkey);
271                                 pkey = tmp_pkey;
272                                 found = true;
273                                 break;
274                         }
275                 }
276
277                 if (!found) {
278                         pkey->pkey_index = pp->pkey_index;
279                         spin_lock_init(&pkey->qp_list_lock);
280                         INIT_LIST_HEAD(&pkey->qp_list);
281                         list_add(&pkey->pkey_index_list,
282                                  &dev->port_pkey_list[port_num].pkey_list);
283                 }
284                 spin_unlock(&dev->port_pkey_list[port_num].list_lock);
285         }
286
287         spin_lock(&pkey->qp_list_lock);
288         list_add(&pp->qp_list, &pkey->qp_list);
289         spin_unlock(&pkey->qp_list_lock);
290
291         pp->state = IB_PORT_PKEY_LISTED;
292
293         return ret;
294 }
295
296 /* The caller of this function must hold the QP security
297  * mutex.
298  */
299 static void port_pkey_list_remove(struct ib_port_pkey *pp)
300 {
301         struct pkey_index_qp_list *pkey;
302
303         if (pp->state != IB_PORT_PKEY_LISTED)
304                 return;
305
306         pkey = get_pkey_idx_qp_list(pp);
307
308         spin_lock(&pkey->qp_list_lock);
309         list_del(&pp->qp_list);
310         spin_unlock(&pkey->qp_list_lock);
311
312         /* The setting may still be valid, i.e. after
313          * a destroy has failed for example.
314          */
315         pp->state = IB_PORT_PKEY_VALID;
316 }
317
318 static void destroy_qp_security(struct ib_qp_security *sec)
319 {
320         security_ib_free_security(sec->security);
321         kfree(sec->ports_pkeys);
322         kfree(sec);
323 }
324
325 /* The caller of this function must hold the QP security
326  * mutex.
327  */
328 static struct ib_ports_pkeys *get_new_pps(const struct ib_qp *qp,
329                                           const struct ib_qp_attr *qp_attr,
330                                           int qp_attr_mask)
331 {
332         struct ib_ports_pkeys *new_pps;
333         struct ib_ports_pkeys *qp_pps = qp->qp_sec->ports_pkeys;
334
335         new_pps = kzalloc(sizeof(*new_pps), GFP_KERNEL);
336         if (!new_pps)
337                 return NULL;
338
339         if (qp_attr_mask & (IB_QP_PKEY_INDEX | IB_QP_PORT)) {
340                 if (!qp_pps) {
341                         new_pps->main.port_num = qp_attr->port_num;
342                         new_pps->main.pkey_index = qp_attr->pkey_index;
343                 } else {
344                         new_pps->main.port_num = (qp_attr_mask & IB_QP_PORT) ?
345                                                   qp_attr->port_num :
346                                                   qp_pps->main.port_num;
347
348                         new_pps->main.pkey_index =
349                                         (qp_attr_mask & IB_QP_PKEY_INDEX) ?
350                                          qp_attr->pkey_index :
351                                          qp_pps->main.pkey_index;
352                 }
353                 new_pps->main.state = IB_PORT_PKEY_VALID;
354         } else if (qp_pps) {
355                 new_pps->main.port_num = qp_pps->main.port_num;
356                 new_pps->main.pkey_index = qp_pps->main.pkey_index;
357                 if (qp_pps->main.state != IB_PORT_PKEY_NOT_VALID)
358                         new_pps->main.state = IB_PORT_PKEY_VALID;
359         }
360
361         if (qp_attr_mask & IB_QP_ALT_PATH) {
362                 new_pps->alt.port_num = qp_attr->alt_port_num;
363                 new_pps->alt.pkey_index = qp_attr->alt_pkey_index;
364                 new_pps->alt.state = IB_PORT_PKEY_VALID;
365         } else if (qp_pps) {
366                 new_pps->alt.port_num = qp_pps->alt.port_num;
367                 new_pps->alt.pkey_index = qp_pps->alt.pkey_index;
368                 if (qp_pps->alt.state != IB_PORT_PKEY_NOT_VALID)
369                         new_pps->alt.state = IB_PORT_PKEY_VALID;
370         }
371
372         new_pps->main.sec = qp->qp_sec;
373         new_pps->alt.sec = qp->qp_sec;
374         return new_pps;
375 }
376
377 int ib_open_shared_qp_security(struct ib_qp *qp, struct ib_device *dev)
378 {
379         struct ib_qp *real_qp = qp->real_qp;
380         int ret;
381
382         ret = ib_create_qp_security(qp, dev);
383
384         if (ret)
385                 return ret;
386
387         mutex_lock(&real_qp->qp_sec->mutex);
388         ret = check_qp_port_pkey_settings(real_qp->qp_sec->ports_pkeys,
389                                           qp->qp_sec);
390
391         if (ret)
392                 goto ret;
393
394         if (qp != real_qp)
395                 list_add(&qp->qp_sec->shared_qp_list,
396                          &real_qp->qp_sec->shared_qp_list);
397 ret:
398         mutex_unlock(&real_qp->qp_sec->mutex);
399         if (ret)
400                 destroy_qp_security(qp->qp_sec);
401
402         return ret;
403 }
404
405 void ib_close_shared_qp_security(struct ib_qp_security *sec)
406 {
407         struct ib_qp *real_qp = sec->qp->real_qp;
408
409         mutex_lock(&real_qp->qp_sec->mutex);
410         list_del(&sec->shared_qp_list);
411         mutex_unlock(&real_qp->qp_sec->mutex);
412
413         destroy_qp_security(sec);
414 }
415
416 int ib_create_qp_security(struct ib_qp *qp, struct ib_device *dev)
417 {
418         int ret;
419
420         qp->qp_sec = kzalloc(sizeof(*qp->qp_sec), GFP_KERNEL);
421         if (!qp->qp_sec)
422                 return -ENOMEM;
423
424         qp->qp_sec->qp = qp;
425         qp->qp_sec->dev = dev;
426         mutex_init(&qp->qp_sec->mutex);
427         INIT_LIST_HEAD(&qp->qp_sec->shared_qp_list);
428         atomic_set(&qp->qp_sec->error_list_count, 0);
429         init_completion(&qp->qp_sec->error_complete);
430         ret = security_ib_alloc_security(&qp->qp_sec->security);
431         if (ret)
432                 kfree(qp->qp_sec);
433
434         return ret;
435 }
436 EXPORT_SYMBOL(ib_create_qp_security);
437
438 void ib_destroy_qp_security_begin(struct ib_qp_security *sec)
439 {
440         mutex_lock(&sec->mutex);
441
442         /* Remove the QP from the lists so it won't get added to
443          * a to_error_list during the destroy process.
444          */
445         if (sec->ports_pkeys) {
446                 port_pkey_list_remove(&sec->ports_pkeys->main);
447                 port_pkey_list_remove(&sec->ports_pkeys->alt);
448         }
449
450         /* If the QP is already in one or more of those lists
451          * the destroying flag will ensure the to error flow
452          * doesn't operate on an undefined QP.
453          */
454         sec->destroying = true;
455
456         /* Record the error list count to know how many completions
457          * to wait for.
458          */
459         sec->error_comps_pending = atomic_read(&sec->error_list_count);
460
461         mutex_unlock(&sec->mutex);
462 }
463
464 void ib_destroy_qp_security_abort(struct ib_qp_security *sec)
465 {
466         int ret;
467         int i;
468
469         /* If a concurrent cache update is in progress this
470          * QP security could be marked for an error state
471          * transition.  Wait for this to complete.
472          */
473         for (i = 0; i < sec->error_comps_pending; i++)
474                 wait_for_completion(&sec->error_complete);
475
476         mutex_lock(&sec->mutex);
477         sec->destroying = false;
478
479         /* Restore the position in the lists and verify
480          * access is still allowed in case a cache update
481          * occurred while attempting to destroy.
482          *
483          * Because these setting were listed already
484          * and removed during ib_destroy_qp_security_begin
485          * we know the pkey_index_qp_list for the PKey
486          * already exists so port_pkey_list_insert won't fail.
487          */
488         if (sec->ports_pkeys) {
489                 port_pkey_list_insert(&sec->ports_pkeys->main);
490                 port_pkey_list_insert(&sec->ports_pkeys->alt);
491         }
492
493         ret = check_qp_port_pkey_settings(sec->ports_pkeys, sec);
494         if (ret)
495                 qp_to_error(sec);
496
497         mutex_unlock(&sec->mutex);
498 }
499
500 void ib_destroy_qp_security_end(struct ib_qp_security *sec)
501 {
502         int i;
503
504         /* If a concurrent cache update is occurring we must
505          * wait until this QP security structure is processed
506          * in the QP to error flow before destroying it because
507          * the to_error_list is in use.
508          */
509         for (i = 0; i < sec->error_comps_pending; i++)
510                 wait_for_completion(&sec->error_complete);
511
512         destroy_qp_security(sec);
513 }
514
515 void ib_security_cache_change(struct ib_device *device,
516                               u8 port_num,
517                               u64 subnet_prefix)
518 {
519         struct pkey_index_qp_list *pkey;
520
521         list_for_each_entry(pkey,
522                             &device->port_pkey_list[port_num].pkey_list,
523                             pkey_index_list) {
524                 check_pkey_qps(pkey,
525                                device,
526                                port_num,
527                                subnet_prefix);
528         }
529 }
530
531 void ib_security_destroy_port_pkey_list(struct ib_device *device)
532 {
533         struct pkey_index_qp_list *pkey, *tmp_pkey;
534         int i;
535
536         for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
537                 spin_lock(&device->port_pkey_list[i].list_lock);
538                 list_for_each_entry_safe(pkey,
539                                          tmp_pkey,
540                                          &device->port_pkey_list[i].pkey_list,
541                                          pkey_index_list) {
542                         list_del(&pkey->pkey_index_list);
543                         kfree(pkey);
544                 }
545                 spin_unlock(&device->port_pkey_list[i].list_lock);
546         }
547 }
548
549 int ib_security_modify_qp(struct ib_qp *qp,
550                           struct ib_qp_attr *qp_attr,
551                           int qp_attr_mask,
552                           struct ib_udata *udata)
553 {
554         int ret = 0;
555         struct ib_ports_pkeys *tmp_pps;
556         struct ib_ports_pkeys *new_pps;
557         bool special_qp = (qp->qp_type == IB_QPT_SMI ||
558                            qp->qp_type == IB_QPT_GSI ||
559                            qp->qp_type >= IB_QPT_RESERVED1);
560         bool pps_change = ((qp_attr_mask & (IB_QP_PKEY_INDEX | IB_QP_PORT)) ||
561                            (qp_attr_mask & IB_QP_ALT_PATH));
562
563         if (pps_change && !special_qp) {
564                 mutex_lock(&qp->qp_sec->mutex);
565                 new_pps = get_new_pps(qp,
566                                       qp_attr,
567                                       qp_attr_mask);
568
569                 /* Add this QP to the lists for the new port
570                  * and pkey settings before checking for permission
571                  * in case there is a concurrent cache update
572                  * occurring.  Walking the list for a cache change
573                  * doesn't acquire the security mutex unless it's
574                  * sending the QP to error.
575                  */
576                 ret = port_pkey_list_insert(&new_pps->main);
577
578                 if (!ret)
579                         ret = port_pkey_list_insert(&new_pps->alt);
580
581                 if (!ret)
582                         ret = check_qp_port_pkey_settings(new_pps,
583                                                           qp->qp_sec);
584         }
585
586         if (!ret)
587                 ret = qp->device->modify_qp(qp->real_qp,
588                                             qp_attr,
589                                             qp_attr_mask,
590                                             udata);
591
592         if (pps_change && !special_qp) {
593                 /* Clean up the lists and free the appropriate
594                  * ports_pkeys structure.
595                  */
596                 if (ret) {
597                         tmp_pps = new_pps;
598                 } else {
599                         tmp_pps = qp->qp_sec->ports_pkeys;
600                         qp->qp_sec->ports_pkeys = new_pps;
601                 }
602
603                 if (tmp_pps) {
604                         port_pkey_list_remove(&tmp_pps->main);
605                         port_pkey_list_remove(&tmp_pps->alt);
606                 }
607                 kfree(tmp_pps);
608                 mutex_unlock(&qp->qp_sec->mutex);
609         }
610         return ret;
611 }
612 EXPORT_SYMBOL(ib_security_modify_qp);
613
614 int ib_security_pkey_access(struct ib_device *dev,
615                             u8 port_num,
616                             u16 pkey_index,
617                             void *sec)
618 {
619         u64 subnet_prefix;
620         u16 pkey;
621         int ret;
622
623         ret = ib_get_cached_pkey(dev, port_num, pkey_index, &pkey);
624         if (ret)
625                 return ret;
626
627         ret = ib_get_cached_subnet_prefix(dev, port_num, &subnet_prefix);
628
629         if (ret)
630                 return ret;
631
632         return security_ib_pkey_access(sec, subnet_prefix, pkey);
633 }
634 EXPORT_SYMBOL(ib_security_pkey_access);
635
636 static int ib_mad_agent_security_change(struct notifier_block *nb,
637                                         unsigned long event,
638                                         void *data)
639 {
640         struct ib_mad_agent *ag = container_of(nb, struct ib_mad_agent, lsm_nb);
641
642         if (event != LSM_POLICY_CHANGE)
643                 return NOTIFY_DONE;
644
645         ag->smp_allowed = !security_ib_endport_manage_subnet(ag->security,
646                                                              ag->device->name,
647                                                              ag->port_num);
648
649         return NOTIFY_OK;
650 }
651
652 int ib_mad_agent_security_setup(struct ib_mad_agent *agent,
653                                 enum ib_qp_type qp_type)
654 {
655         int ret;
656
657         ret = security_ib_alloc_security(&agent->security);
658         if (ret)
659                 return ret;
660
661         if (qp_type != IB_QPT_SMI)
662                 return 0;
663
664         ret = security_ib_endport_manage_subnet(agent->security,
665                                                 agent->device->name,
666                                                 agent->port_num);
667         if (ret)
668                 return ret;
669
670         agent->lsm_nb.notifier_call = ib_mad_agent_security_change;
671         ret = register_lsm_notifier(&agent->lsm_nb);
672         if (ret)
673                 return ret;
674
675         agent->smp_allowed = true;
676         agent->lsm_nb_reg = true;
677         return 0;
678 }
679
680 void ib_mad_agent_security_cleanup(struct ib_mad_agent *agent)
681 {
682         security_ib_free_security(agent->security);
683         if (agent->lsm_nb_reg)
684                 unregister_lsm_notifier(&agent->lsm_nb);
685 }
686
687 int ib_mad_enforce_security(struct ib_mad_agent_private *map, u16 pkey_index)
688 {
689         int ret;
690
691         if (map->agent.qp->qp_type == IB_QPT_SMI && !map->agent.smp_allowed)
692                 return -EACCES;
693
694         ret = ib_security_pkey_access(map->agent.device,
695                                       map->agent.port_num,
696                                       pkey_index,
697                                       map->agent.security);
698
699         if (ret)
700                 return ret;
701
702         return 0;
703 }
704
705 #endif /* CONFIG_SECURITY_INFINIBAND */