| 1 |
/* |
| 2 |
* User Accounting |
| 3 |
* |
| 4 |
* Different users can communicate via the broker, and some resources are |
| 5 |
* shared between multiple users. The User object represents the UID of a |
| 6 |
* user, like "struct user_struct" does in the kernel. It is used to account |
| 7 |
* global resources, apply limits, and calculate quotas if different UIDs |
| 8 |
* communicate with each other. |
| 9 |
* |
| 10 |
* All dynamic resources have global per-user limits, which cannot be exceeded |
| 11 |
* by a user. They prevent a single user from exhausting local resources. Each |
| 12 |
* peer that is created is always owned by the user that initialized it. All |
| 13 |
* resources allocated on that peer are accounted on that pinned user. |
| 14 |
* |
| 15 |
* Since the broker allows communication across UID boundaries, any such |
| 16 |
* transmission of resources must be properly accounted. The broker employs |
| 17 |
* dynamic quotas to fairly distribute available resources. Those quotas make |
| 18 |
* sure that available resources of a peer cannot be exhausted by remote UIDs, |
| 19 |
* but are fairly divided among all communicating peers. The share granted to |
| 20 |
* each remote UID is between 1/n and 1/n^2 of the total amount of resources |
| 21 |
* available to the local UID, where n is the number of UIDs consuming a share |
| 22 |
* of the local UID's resources at the time of accounting. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <c-stdaux.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include "util/error.h" |
| 29 |
#include "util/log.h" |
| 30 |
#include "util/ref.h" |
| 31 |
#include "util/user.h" |
| 32 |
|
| 33 |
static void user_usage_link(UserUsage *usage, CRBNode *parent, CRBNode **slot) { |
| 34 |
++usage->user->n_usages; |
| 35 |
c_rbtree_add(&usage->user->usage_tree, parent, slot, &usage->user_node); |
| 36 |
} |
| 37 |
|
| 38 |
static void user_usage_unlink(UserUsage *usage) { |
| 39 |
c_rbnode_unlink(&usage->user_node); |
| 40 |
--usage->user->n_usages; |
| 41 |
} |
| 42 |
|
| 43 |
static int user_usage_new(UserUsage **usagep, User *user, uid_t uid) { |
| 44 |
UserUsage *usage; |
| 45 |
|
| 46 |
usage = calloc(1, sizeof(*usage) + user->registry->n_slots * sizeof(*usage->slots)); |
| 47 |
if (!usage) |
| 48 |
return error_origin(-ENOMEM); |
| 49 |
|
| 50 |
usage->n_refs = REF_INIT; |
| 51 |
usage->user = user_ref(user); |
| 52 |
usage->uid = uid; |
| 53 |
usage->user_node = (CRBNode)C_RBNODE_INIT(usage->user_node); |
| 54 |
|
| 55 |
*usagep = usage; |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static void user_usage_free(_Atomic unsigned long *n_refs, void *userdata) { |
| 60 |
UserUsage *usage = c_container_of(n_refs, UserUsage, n_refs); |
| 61 |
size_t i; |
| 62 |
|
| 63 |
for (i = 0; i < usage->user->registry->n_slots; ++i) |
| 64 |
c_assert(!usage->slots[i]); |
| 65 |
|
| 66 |
user_usage_unlink(usage); |
| 67 |
user_unref(usage->user); |
| 68 |
free(usage); |
| 69 |
} |
| 70 |
|
| 71 |
static UserUsage *user_usage_ref(UserUsage *usage) { |
| 72 |
if (usage) |
| 73 |
ref_inc(&usage->n_refs); |
| 74 |
return usage; |
| 75 |
} |
| 76 |
|
| 77 |
static UserUsage *user_usage_unref(UserUsage *usage) { |
| 78 |
if (usage) |
| 79 |
ref_dec(&usage->n_refs, user_usage_free, NULL); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
C_DEFINE_CLEANUP(UserUsage *, user_usage_unref); |
| 84 |
|
| 85 |
static int user_usage_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 86 |
UserUsage *usage = c_container_of(rb, UserUsage, user_node); |
| 87 |
uid_t uid = *(uid_t*)k; |
| 88 |
|
| 89 |
if (uid < usage->uid) |
| 90 |
return -1; |
| 91 |
if (uid > usage->uid) |
| 92 |
return 1; |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* user_charge_init() - initialize charge object |
| 99 |
* @charge: charge object to initialize |
| 100 |
* |
| 101 |
* This initializes a new charge object. |
| 102 |
*/ |
| 103 |
void user_charge_init(UserCharge *charge) { |
| 104 |
*charge = (UserCharge){}; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* user_charge_deinit() - destroy charge object |
| 109 |
* @charge: charge object to destroy |
| 110 |
* |
| 111 |
* This destroys a charge object that was previously initialized via |
| 112 |
* user_charge_init(). If the object was never charged, this is a no-op. |
| 113 |
* Otherwise, the charge is released and the object is re-initialized. |
| 114 |
*/ |
| 115 |
void user_charge_deinit(UserCharge *charge) { |
| 116 |
if (charge->usage) { |
| 117 |
charge->usage->user->slots[charge->slot].n += charge->charge; |
| 118 |
charge->usage->slots[charge->slot] -= charge->charge; |
| 119 |
|
| 120 |
charge->usage = user_usage_unref(charge->usage); |
| 121 |
charge->slot = 0; |
| 122 |
charge->charge = 0; |
| 123 |
} else { |
| 124 |
c_assert(!charge->charge); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static int user_charge_check(unsigned int remaining, |
| 129 |
unsigned int users, |
| 130 |
unsigned int share, |
| 131 |
unsigned int charge) { |
| 132 |
unsigned int limit; |
| 133 |
|
| 134 |
/* |
| 135 |
* For a given allocation scheme `A(users)`, a single allocation is |
| 136 |
* allowed to request anything less than, or equal to: |
| 137 |
* |
| 138 |
* remaining + share |
| 139 |
* charge <= ~~~~~~~~~~~~~~~~~ - share |
| 140 |
* A(users) |
| 141 |
* |
| 142 |
* We want to avoid a division, so instead we modify the equation to |
| 143 |
* end up as: |
| 144 |
* |
| 145 |
* A(users) * (charge + share) - share <= remaining |
| 146 |
* |
| 147 |
* Note that currently we use an allocator with polynomial guarantees: |
| 148 |
* |
| 149 |
* A(users) = users + 1 |
| 150 |
*/ |
| 151 |
|
| 152 |
if (!__builtin_add_overflow(share, charge, &limit) && |
| 153 |
!__builtin_mul_overflow(limit, users + 1, &limit) && |
| 154 |
!__builtin_sub_overflow(limit, share, &limit) && |
| 155 |
limit <= remaining) |
| 156 |
return 0; |
| 157 |
|
| 158 |
return USER_E_QUOTA; |
| 159 |
} |
| 160 |
|
| 161 |
static void user_link(User *user, CRBNode *parent, CRBNode **slot) { |
| 162 |
c_rbtree_add(&user->registry->user_tree, parent, slot, &user->registry_node); |
| 163 |
} |
| 164 |
|
| 165 |
static void user_unlink(User *user) { |
| 166 |
c_rbnode_unlink(&user->registry_node); |
| 167 |
} |
| 168 |
|
| 169 |
static int user_new(User **userp, UserRegistry *registry, uid_t uid) { |
| 170 |
User *user; |
| 171 |
size_t i; |
| 172 |
|
| 173 |
user = calloc(1, sizeof(*user) + registry->n_slots * sizeof(*user->slots)); |
| 174 |
if (!user) |
| 175 |
return error_origin(-ENOMEM); |
| 176 |
|
| 177 |
user->n_refs = REF_INIT; |
| 178 |
user->registry = registry; |
| 179 |
user->uid = uid; |
| 180 |
user->registry_node = (CRBNode)C_RBNODE_INIT(user->registry_node); |
| 181 |
user->usage_tree = (CRBTree)C_RBTREE_INIT; |
| 182 |
|
| 183 |
for (i = 0; i < registry->n_slots; ++i) { |
| 184 |
user->slots[i].max = registry->maxima[i]; |
| 185 |
user->slots[i].n = user->slots[i].max; |
| 186 |
} |
| 187 |
|
| 188 |
*userp = user; |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
void user_free(_Atomic unsigned long *n_refs, void *userdata) { |
| 193 |
User *user = c_container_of(n_refs, User, n_refs); |
| 194 |
size_t i; |
| 195 |
|
| 196 |
c_assert(c_rbtree_is_empty(&user->usage_tree)); |
| 197 |
c_assert(user->n_usages == 0); |
| 198 |
|
| 199 |
for (i = 0; i < user->registry->n_slots; ++i) |
| 200 |
c_assert(user->slots[i].n == user->slots[i].max); |
| 201 |
|
| 202 |
user_unlink(user); |
| 203 |
free(user); |
| 204 |
} |
| 205 |
|
| 206 |
static int user_ref_usage(User *user, UserUsage **usagep, User *actor) { |
| 207 |
UserUsage *usage; |
| 208 |
CRBNode **slot, *parent; |
| 209 |
int r; |
| 210 |
|
| 211 |
slot = c_rbtree_find_slot(&user->usage_tree, user_usage_compare, &actor->uid, &parent); |
| 212 |
if (slot) { |
| 213 |
r = user_usage_new(&usage, user, actor->uid); |
| 214 |
if (r) |
| 215 |
return r; |
| 216 |
|
| 217 |
user_usage_link(usage, parent, slot); |
| 218 |
} else { |
| 219 |
usage = c_container_of(parent, UserUsage, user_node); |
| 220 |
user_usage_ref(usage); |
| 221 |
} |
| 222 |
|
| 223 |
*usagep = usage; |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
static int user_charge_commit_log(Log *log, User *user, UserCharge *charge, User *actor, size_t slot, unsigned int amount) { |
| 228 |
int r; |
| 229 |
|
| 230 |
if (!log) |
| 231 |
return 0; |
| 232 |
|
| 233 |
if (charge->usage->logged) |
| 234 |
return 0; |
| 235 |
|
| 236 |
c_assert(slot < user->registry->n_slots); |
| 237 |
|
| 238 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_USER=%u\n", user->uid); |
| 239 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_ACTOR=%u\n", actor->uid); |
| 240 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_AMOUNT=%u\n", amount); |
| 241 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_N_ACTORS=%u\n", user->n_usages); |
| 242 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_SLOT=%s\n", user_slot_to_string(slot)); |
| 243 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_REMAINING=%u\n", user->slots[slot].n); |
| 244 |
log_appendf(log, "DBUS_BROKER_USER_CHARGE_CONSUMED=%u\n", charge->usage->slots[slot]); |
| 245 |
|
| 246 |
r = log_commitf(log, "UID %u exceeded its '%s' quota on UID %u.", actor->uid, user_slot_to_string(slot), user->uid); |
| 247 |
if (r) |
| 248 |
return error_fold(r); |
| 249 |
|
| 250 |
charge->usage->logged = true; |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* user_charge() - charge a user object |
| 257 |
* @user: user object to charge |
| 258 |
* @charge: charge object used to record the charge |
| 259 |
* @actor: user object charged on behalf of, or NULL |
| 260 |
* @slot: slot to charge |
| 261 |
* @amount: charge amount |
| 262 |
* |
| 263 |
* Charge @amount units on slot @slot on behalf of @actor. Record the charge in |
| 264 |
* @charge so it can later be undone. |
| 265 |
* |
| 266 |
* @charge must be initialized and either be unused or already charged on the |
| 267 |
* @user + @actor combination. |
| 268 |
* |
| 269 |
* @actor is at most allowed to consume an n'th of @user's resources that have |
| 270 |
* not been consumed by any other user, where n is one more than the total |
| 271 |
* number of actors currently pinning any of @user's resources. If this quota |
| 272 |
* is exceeded the charge fails to apply and nothing is charged. The first time |
| 273 |
* a certain quota fails for a certain @user / @actor a message is written to |
| 274 |
* the log. |
| 275 |
* |
| 276 |
* If @actor is NULL it is taken to be @user itself. |
| 277 |
* |
| 278 |
* If @user is NULL, this is a no-op and @charge stays untouched. |
| 279 |
* |
| 280 |
* Return: 0 on success, error code on failure. |
| 281 |
*/ |
| 282 |
int user_charge(User *user, UserCharge *charge, User *actor, size_t slot, unsigned int amount) { |
| 283 |
_c_cleanup_(user_usage_unrefp) UserUsage *usage = NULL; |
| 284 |
unsigned int *user_slot, *usage_slot; |
| 285 |
int r; |
| 286 |
|
| 287 |
/* no charge, no work */ |
| 288 |
if (!amount) |
| 289 |
return 0; |
| 290 |
|
| 291 |
/* excluded from accounting */ |
| 292 |
if (!user) |
| 293 |
return 0; |
| 294 |
|
| 295 |
/* charge to itself */ |
| 296 |
if (!actor) |
| 297 |
actor = user; |
| 298 |
|
| 299 |
if (charge->usage) { |
| 300 |
c_assert(user == charge->usage->user); |
| 301 |
c_assert(actor->uid == charge->usage->uid); |
| 302 |
c_assert(slot == charge->slot); |
| 303 |
usage = user_usage_ref(charge->usage); |
| 304 |
} else { |
| 305 |
r = user_ref_usage(user, &usage, actor); |
| 306 |
if (r) |
| 307 |
return error_trace(r); |
| 308 |
} |
| 309 |
|
| 310 |
c_assert(slot < user->registry->n_slots); |
| 311 |
user_slot = &user->slots[slot].n; |
| 312 |
usage_slot = &usage->slots[slot]; |
| 313 |
|
| 314 |
if (user == actor) { |
| 315 |
/* never apply quotas on self-charge */ |
| 316 |
if (amount > *user_slot) |
| 317 |
goto quota; |
| 318 |
} else { |
| 319 |
r = user_charge_check(*user_slot, user->n_usages, *usage_slot, amount); |
| 320 |
if (r) { |
| 321 |
if (r == USER_E_QUOTA) |
| 322 |
goto quota; |
| 323 |
|
| 324 |
return error_trace(r); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
*user_slot -= amount; |
| 329 |
*usage_slot += amount; |
| 330 |
charge->charge += amount; |
| 331 |
|
| 332 |
if (!charge->usage) { |
| 333 |
charge->slot = slot; |
| 334 |
charge->usage = usage; |
| 335 |
usage = NULL; |
| 336 |
} |
| 337 |
|
| 338 |
return 0; |
| 339 |
quota: |
| 340 |
if (!charge->usage) { |
| 341 |
charge->slot = slot; |
| 342 |
charge->usage = usage; |
| 343 |
usage = NULL; |
| 344 |
} |
| 345 |
|
| 346 |
r = user_charge_commit_log(user->registry->log, user, charge, actor, slot, amount); |
| 347 |
if (r) |
| 348 |
return error_trace(r); |
| 349 |
|
| 350 |
return USER_E_QUOTA; |
| 351 |
} |
| 352 |
|
| 353 |
static int user_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 354 |
User *user = c_container_of(rb, User, registry_node); |
| 355 |
uid_t uid = *(uid_t*)k; |
| 356 |
|
| 357 |
if (uid < user->uid) |
| 358 |
return -1; |
| 359 |
if (uid > user->uid) |
| 360 |
return 1; |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* user_registry_init() - initialize user registry |
| 367 |
* @registry: user registry to operate on |
| 368 |
* @log: destination of any log messages |
| 369 |
* @n_slots: number of accounting slots |
| 370 |
* @maxima: maxima for each slot |
| 371 |
* |
| 372 |
* Initialize a user registry. @n_slots defines the number of distinct |
| 373 |
* accounting slots that will be available on all users on that registry. |
| 374 |
* |
| 375 |
* Return: 0 on success, negative error code on failure. |
| 376 |
*/ |
| 377 |
int user_registry_init(UserRegistry *registry, |
| 378 |
Log *log, |
| 379 |
size_t n_slots, |
| 380 |
const unsigned int *maxima) { |
| 381 |
static_assert(sizeof(*maxima) == sizeof(*registry->maxima), |
| 382 |
"Type mismatch for maxima"); |
| 383 |
|
| 384 |
*registry = (UserRegistry)USER_REGISTRY_NULL; |
| 385 |
|
| 386 |
registry->maxima = calloc(n_slots, sizeof(*registry->maxima)); |
| 387 |
if (!registry->maxima) |
| 388 |
return error_origin(-ENOMEM); |
| 389 |
|
| 390 |
registry->log = log; |
| 391 |
registry->n_slots = n_slots; |
| 392 |
c_memcpy(registry->maxima, maxima, n_slots * sizeof(*registry->maxima)); |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* user_registry_deinit() - destroy user registry |
| 399 |
* @registry: user registry to operate on |
| 400 |
* |
| 401 |
* This destroys a user registry, previously initialized via |
| 402 |
* user_registry_init(). All user elements instantiated from the registry must |
| 403 |
* have been destroyed before the registry is deinitialized. |
| 404 |
*/ |
| 405 |
void user_registry_deinit(UserRegistry *registry) { |
| 406 |
c_assert(c_rbtree_is_empty(®istry->user_tree)); |
| 407 |
|
| 408 |
free(registry->maxima); |
| 409 |
*registry = (UserRegistry)USER_REGISTRY_NULL; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* user_registry_ref_user() - search user in registry |
| 414 |
* @registry: registry to query |
| 415 |
* @userp: output argument for user object |
| 416 |
* @uid: uid of user to search for |
| 417 |
* |
| 418 |
* This searches for a user object with UID @uid in @registry, takes a |
| 419 |
* reference and returns it in @userp. |
| 420 |
* |
| 421 |
* Return: 0 on success, error code on failure. |
| 422 |
*/ |
| 423 |
int user_registry_ref_user(UserRegistry *registry, User **userp, uid_t uid) { |
| 424 |
User *user; |
| 425 |
CRBNode **slot, *parent; |
| 426 |
int r; |
| 427 |
|
| 428 |
slot = c_rbtree_find_slot(®istry->user_tree, user_compare, &uid, &parent); |
| 429 |
if (slot) { |
| 430 |
r = user_new(&user, registry, uid); |
| 431 |
if (r) |
| 432 |
return error_trace(r); |
| 433 |
|
| 434 |
user_link(user, parent, slot); |
| 435 |
} else { |
| 436 |
user = c_container_of(parent, User, registry_node); |
| 437 |
user_ref(user); |
| 438 |
} |
| 439 |
|
| 440 |
*userp = user; |
| 441 |
return 0; |
| 442 |
} |