map_in_map.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (c) 2017 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/bpf.h>
  9. #include "map_in_map.h"
  10. struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
  11. {
  12. struct bpf_map *inner_map, *inner_map_meta;
  13. u32 inner_map_meta_size;
  14. struct fd f;
  15. f = fdget(inner_map_ufd);
  16. inner_map = __bpf_map_get(f);
  17. if (IS_ERR(inner_map))
  18. return inner_map;
  19. /* prog_array->owner_prog_type and owner_jited
  20. * is a runtime binding. Doing static check alone
  21. * in the verifier is not enough.
  22. */
  23. if (inner_map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
  24. inner_map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE) {
  25. fdput(f);
  26. return ERR_PTR(-ENOTSUPP);
  27. }
  28. /* Does not support >1 level map-in-map */
  29. if (inner_map->inner_map_meta) {
  30. fdput(f);
  31. return ERR_PTR(-EINVAL);
  32. }
  33. inner_map_meta_size = sizeof(*inner_map_meta);
  34. /* In some cases verifier needs to access beyond just base map. */
  35. if (inner_map->ops == &array_map_ops)
  36. inner_map_meta_size = sizeof(struct bpf_array);
  37. inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
  38. if (!inner_map_meta) {
  39. fdput(f);
  40. return ERR_PTR(-ENOMEM);
  41. }
  42. inner_map_meta->map_type = inner_map->map_type;
  43. inner_map_meta->key_size = inner_map->key_size;
  44. inner_map_meta->value_size = inner_map->value_size;
  45. inner_map_meta->map_flags = inner_map->map_flags;
  46. inner_map_meta->max_entries = inner_map->max_entries;
  47. /* Misc members not needed in bpf_map_meta_equal() check. */
  48. inner_map_meta->ops = inner_map->ops;
  49. if (inner_map->ops == &array_map_ops) {
  50. inner_map_meta->unpriv_array = inner_map->unpriv_array;
  51. container_of(inner_map_meta, struct bpf_array, map)->index_mask =
  52. container_of(inner_map, struct bpf_array, map)->index_mask;
  53. }
  54. fdput(f);
  55. return inner_map_meta;
  56. }
  57. void bpf_map_meta_free(struct bpf_map *map_meta)
  58. {
  59. kfree(map_meta);
  60. }
  61. bool bpf_map_meta_equal(const struct bpf_map *meta0,
  62. const struct bpf_map *meta1)
  63. {
  64. /* No need to compare ops because it is covered by map_type */
  65. return meta0->map_type == meta1->map_type &&
  66. meta0->key_size == meta1->key_size &&
  67. meta0->value_size == meta1->value_size &&
  68. meta0->map_flags == meta1->map_flags &&
  69. meta0->max_entries == meta1->max_entries;
  70. }
  71. void *bpf_map_fd_get_ptr(struct bpf_map *map,
  72. struct file *map_file /* not used */,
  73. int ufd)
  74. {
  75. struct bpf_map *inner_map;
  76. struct fd f;
  77. f = fdget(ufd);
  78. inner_map = __bpf_map_get(f);
  79. if (IS_ERR(inner_map))
  80. return inner_map;
  81. if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
  82. inner_map = bpf_map_inc(inner_map, false);
  83. else
  84. inner_map = ERR_PTR(-EINVAL);
  85. fdput(f);
  86. return inner_map;
  87. }
  88. void bpf_map_fd_put_ptr(void *ptr)
  89. {
  90. /* ptr->ops->map_free() has to go through one
  91. * rcu grace period by itself.
  92. */
  93. bpf_map_put(ptr);
  94. }
  95. u32 bpf_map_fd_sys_lookup_elem(void *ptr)
  96. {
  97. return ((struct bpf_map *)ptr)->id;
  98. }