| 1 |
/* lookup_table.c -- |
| 2 |
* Copyright 2009, 2013, 2025 Red Hat Inc. |
| 3 |
* All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program; see the file COPYING.LIB. If not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor |
| 18 |
* Boston, MA 02110-1335, USA. |
| 19 |
* |
| 20 |
* Authors: |
| 21 |
* Steve Grubb <[email protected]> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
#include <stddef.h> |
| 26 |
#include <linux/capability.h> |
| 27 |
#include <strings.h> |
| 28 |
#include <stdio.h> |
| 29 |
|
| 30 |
|
| 31 |
#pragma GCC optimize("O3") |
| 32 |
#define hidden __attribute__ ((visibility ("hidden"))) |
| 33 |
extern unsigned int last_cap hidden; |
| 34 |
|
| 35 |
#undef cap_valid |
| 36 |
#define cap_valid(x) ((x) <= last_cap) |
| 37 |
|
| 38 |
|
| 39 |
struct transtab { |
| 40 |
unsigned int value; |
| 41 |
unsigned int offset; |
| 42 |
}; |
| 43 |
|
| 44 |
#define MSGSTRFIELD(line) MSGSTRFIELD1(line) |
| 45 |
#define MSGSTRFIELD1(line) str##line |
| 46 |
|
| 47 |
|
| 48 |
/* To create the following tables in a DSO-friendly way we split them in |
| 49 |
two separate variables: a long string which is created by concatenating |
| 50 |
all strings referenced in the table and the table itself, which uses |
| 51 |
offsets instead of string pointers. To do this without increasing |
| 52 |
the maintenance burden we use a lot of preprocessor magic. All the |
| 53 |
maintainer has to do is to add a new entry to the included file and |
| 54 |
recompile. */ |
| 55 |
|
| 56 |
static const union captab_msgstr_t { |
| 57 |
struct { |
| 58 |
#define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof (s)]; |
| 59 |
#include "captab.h" |
| 60 |
#undef _S |
| 61 |
}; |
| 62 |
char str[0]; |
| 63 |
} captab_msgstr = { { |
| 64 |
#define _S(n, s) s, |
| 65 |
#include "captab.h" |
| 66 |
#undef _S |
| 67 |
} }; |
| 68 |
static const struct transtab captab[] = { |
| 69 |
#define _S(n, s) { n, offsetof(union captab_msgstr_t, \ |
| 70 |
MSGSTRFIELD(__LINE__)) }, |
| 71 |
#include "captab.h" |
| 72 |
#undef _S |
| 73 |
}; |
| 74 |
#define CAP_NG_CAPABILITY_NAMES (sizeof(captab)/sizeof(captab[0])) |
| 75 |
|
| 76 |
|
| 77 |
static inline int capng_lookup_name(const char *name) |
| 78 |
{ |
| 79 |
// brute force search |
| 80 |
for (size_t i = 0; i < CAP_NG_CAPABILITY_NAMES; i++) { |
| 81 |
if (!strcasecmp(captab_msgstr.str + captab[i].offset, name)) |
| 82 |
return captab[i].value; |
| 83 |
} |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
static inline const char *capng_lookup_number(unsigned int number) |
| 88 |
{ |
| 89 |
if (number >= CAP_NG_CAPABILITY_NAMES) |
| 90 |
return NULL; |
| 91 |
|
| 92 |
if (captab[number].value == number) |
| 93 |
return captab_msgstr.str + captab[number].offset; |
| 94 |
|
| 95 |
// Fallback to old search in case a capability is retired |
| 96 |
for (size_t i = 0; i < CAP_NG_CAPABILITY_NAMES; i++) { |
| 97 |
if (captab[i].value == number) |
| 98 |
return captab_msgstr.str + captab[i].offset; |
| 99 |
} |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* capng_name_to_capability - given a string with the name of the capabilty |
| 105 |
* return its number. |
| 106 |
* @name - the name of the capability to lookup |
| 107 |
* |
| 108 |
* returns the number associated with the string. |
| 109 |
*/ |
| 110 |
int capng_name_to_capability(const char *name) |
| 111 |
{ |
| 112 |
return capng_lookup_name(name); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* capng_capability_to_name - given a number, return a string with the name of |
| 117 |
* the capabilty. |
| 118 |
* @capability - the number of the capability to lookup |
| 119 |
* |
| 120 |
* returns the string associated with the number. |
| 121 |
*/ |
| 122 |
static __thread char ptr2[32]; |
| 123 |
const char *capng_capability_to_name(unsigned int capability) |
| 124 |
{ |
| 125 |
const char *ptr; |
| 126 |
|
| 127 |
if (!cap_valid(capability)) |
| 128 |
return NULL; |
| 129 |
|
| 130 |
ptr = capng_lookup_number(capability); |
| 131 |
if (ptr == NULL) { |
| 132 |
snprintf(ptr2, sizeof(ptr2), "cap_%u", capability); |
| 133 |
ptr = ptr2; |
| 134 |
} |
| 135 |
return ptr; |
| 136 |
} |
| 137 |
|