linux/security/security.c
<<
>>
Prefs
   1/*
   2 * Security plug functions
   3 *
   4 * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
   5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
   6 * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
   7 * Copyright (C) 2016 Mellanox Technologies
   8 *
   9 *      This program is free software; you can redistribute it and/or modify
  10 *      it under the terms of the GNU General Public License as published by
  11 *      the Free Software Foundation; either version 2 of the License, or
  12 *      (at your option) any later version.
  13 */
  14
  15#define pr_fmt(fmt) "LSM: " fmt
  16
  17#include <linux/bpf.h>
  18#include <linux/capability.h>
  19#include <linux/dcache.h>
  20#include <linux/export.h>
  21#include <linux/init.h>
  22#include <linux/kernel.h>
  23#include <linux/lsm_hooks.h>
  24#include <linux/integrity.h>
  25#include <linux/ima.h>
  26#include <linux/evm.h>
  27#include <linux/fsnotify.h>
  28#include <linux/mman.h>
  29#include <linux/mount.h>
  30#include <linux/personality.h>
  31#include <linux/backing-dev.h>
  32#include <linux/string.h>
  33#include <linux/msg.h>
  34#include <net/flow.h>
  35
  36/* RHEL8-only: work around a KABI false positive */
  37#include RH_KABI_FAKE_INCLUDE(<trace/events/initcall.h>)
  38
  39#define MAX_LSM_EVM_XATTR       2
  40
  41/* How many LSMs were built into the kernel? */
  42#define LSM_COUNT (__end_lsm_info - __start_lsm_info)
  43
  44struct security_hook_heads security_hook_heads __lsm_ro_after_init;
  45static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
  46
  47static struct kmem_cache *lsm_file_cache;
  48static struct kmem_cache *lsm_inode_cache;
  49
  50char *lsm_names;
  51static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init;
  52
  53/* Boot-time LSM user choice */
  54static __initdata const char *chosen_lsm_order;
  55static __initdata const char *chosen_major_lsm;
  56
  57static __initconst const char * const builtin_lsm_order = CONFIG_LSM;
  58
  59/* Ordered list of LSMs to initialize. */
  60static __initdata struct lsm_info **ordered_lsms;
  61static __initdata struct lsm_info *exclusive;
  62
  63static __initdata bool debug;
  64#define init_debug(...)                                         \
  65        do {                                                    \
  66                if (debug)                                      \
  67                        pr_info(__VA_ARGS__);                   \
  68        } while (0)
  69
  70static bool __init is_enabled(struct lsm_info *lsm)
  71{
  72        if (!lsm->enabled)
  73                return false;
  74
  75        return *lsm->enabled;
  76}
  77
  78/* Mark an LSM's enabled flag. */
  79static int lsm_enabled_true __initdata = 1;
  80static int lsm_enabled_false __initdata = 0;
  81static void __init set_enabled(struct lsm_info *lsm, bool enabled)
  82{
  83        /*
  84         * When an LSM hasn't configured an enable variable, we can use
  85         * a hard-coded location for storing the default enabled state.
  86         */
  87        if (!lsm->enabled) {
  88                if (enabled)
  89                        lsm->enabled = &lsm_enabled_true;
  90                else
  91                        lsm->enabled = &lsm_enabled_false;
  92        } else if (lsm->enabled == &lsm_enabled_true) {
  93                if (!enabled)
  94                        lsm->enabled = &lsm_enabled_false;
  95        } else if (lsm->enabled == &lsm_enabled_false) {
  96                if (enabled)
  97                        lsm->enabled = &lsm_enabled_true;
  98        } else {
  99                *lsm->enabled = enabled;
 100        }
 101}
 102
 103/* Is an LSM already listed in the ordered LSMs list? */
 104static bool __init exists_ordered_lsm(struct lsm_info *lsm)
 105{
 106        struct lsm_info **check;
 107
 108        for (check = ordered_lsms; *check; check++)
 109                if (*check == lsm)
 110                        return true;
 111
 112        return false;
 113}
 114
 115/* Append an LSM to the list of ordered LSMs to initialize. */
 116static int last_lsm __initdata;
 117static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
 118{
 119        /* Ignore duplicate selections. */
 120        if (exists_ordered_lsm(lsm))
 121                return;
 122
 123        if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
 124                return;
 125
 126        /* Enable this LSM, if it is not already set. */
 127        if (!lsm->enabled)
 128                lsm->enabled = &lsm_enabled_true;
 129        ordered_lsms[last_lsm++] = lsm;
 130
 131        init_debug("%s ordering: %s (%sabled)\n", from, lsm->name,
 132                   is_enabled(lsm) ? "en" : "dis");
 133}
 134
 135/* Is an LSM allowed to be initialized? */
 136static bool __init lsm_allowed(struct lsm_info *lsm)
 137{
 138        /* Skip if the LSM is disabled. */
 139        if (!is_enabled(lsm))
 140                return false;
 141
 142        /* Not allowed if another exclusive LSM already initialized. */
 143        if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
 144                init_debug("exclusive disabled: %s\n", lsm->name);
 145                return false;
 146        }
 147
 148        return true;
 149}
 150
 151static void __init lsm_set_blob_size(int *need, int *lbs)
 152{
 153        int offset;
 154
 155        if (*need > 0) {
 156                offset = *lbs;
 157                *lbs += *need;
 158                *need = offset;
 159        }
 160}
 161
 162static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
 163{
 164        if (!needed)
 165                return;
 166
 167        lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
 168        lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
 169        /*
 170         * The inode blob gets an rcu_head in addition to
 171         * what the modules might need.
 172         */
 173        if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
 174                blob_sizes.lbs_inode = sizeof(struct rcu_head);
 175        lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
 176        lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
 177        lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
 178        lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
 179        lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
 180}
 181
 182/* Prepare LSM for initialization. */
 183static void __init prepare_lsm(struct lsm_info *lsm)
 184{
 185        int enabled = lsm_allowed(lsm);
 186
 187        /* Record enablement (to handle any following exclusive LSMs). */
 188        set_enabled(lsm, enabled);
 189
 190        /* If enabled, do pre-initialization work. */
 191        if (enabled) {
 192                if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
 193                        exclusive = lsm;
 194                        init_debug("exclusive chosen: %s\n", lsm->name);
 195                }
 196
 197                lsm_set_blob_sizes(lsm->blobs);
 198        }
 199}
 200
 201/* Initialize a given LSM, if it is enabled. */
 202static void __init initialize_lsm(struct lsm_info *lsm)
 203{
 204        if (is_enabled(lsm)) {
 205                int ret;
 206
 207                init_debug("initializing %s\n", lsm->name);
 208                ret = lsm->init();
 209                WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
 210        }
 211}
 212
 213/* Populate ordered LSMs list from comma-separated LSM name list. */
 214static void __init ordered_lsm_parse(const char *order, const char *origin)
 215{
 216        struct lsm_info *lsm;
 217        char *sep, *name, *next;
 218
 219        /* LSM_ORDER_FIRST is always first. */
 220        for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
 221                if (lsm->order == LSM_ORDER_FIRST)
 222                        append_ordered_lsm(lsm, "first");
 223        }
 224
 225        /* Process "security=", if given. */
 226        if (chosen_major_lsm) {
 227                struct lsm_info *major;
 228
 229                /*
 230                 * To match the original "security=" behavior, this
 231                 * explicitly does NOT fallback to another Legacy Major
 232                 * if the selected one was separately disabled: disable
 233                 * all non-matching Legacy Major LSMs.
 234                 */
 235                for (major = __start_lsm_info; major < __end_lsm_info;
 236                     major++) {
 237                        if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
 238                            strcmp(major->name, chosen_major_lsm) != 0) {
 239                                set_enabled(major, false);
 240                                init_debug("security=%s disabled: %s\n",
 241                                           chosen_major_lsm, major->name);
 242                        }
 243                }
 244        }
 245
 246        sep = kstrdup(order, GFP_KERNEL);
 247        next = sep;
 248        /* Walk the list, looking for matching LSMs. */
 249        while ((name = strsep(&next, ",")) != NULL) {
 250                bool found = false;
 251
 252                for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
 253                        if (lsm->order == LSM_ORDER_MUTABLE &&
 254                            strcmp(lsm->name, name) == 0) {
 255                                append_ordered_lsm(lsm, origin);
 256                                found = true;
 257                        }
 258                }
 259
 260                if (!found)
 261                        init_debug("%s ignored: %s\n", origin, name);
 262        }
 263
 264        /* Process "security=", if given. */
 265        if (chosen_major_lsm) {
 266                for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
 267                        if (exists_ordered_lsm(lsm))
 268                                continue;
 269                        if (strcmp(lsm->name, chosen_major_lsm) == 0)
 270                                append_ordered_lsm(lsm, "security=");
 271                }
 272        }
 273
 274        /* Disable all LSMs not in the ordered list. */
 275        for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
 276                if (exists_ordered_lsm(lsm))
 277                        continue;
 278                set_enabled(lsm, false);
 279                init_debug("%s disabled: %s\n", origin, lsm->name);
 280        }
 281
 282        kfree(sep);
 283}
 284
 285static void __init lsm_early_cred(struct cred *cred);
 286static void __init lsm_early_task(struct task_struct *task);
 287
 288static void __init ordered_lsm_init(void)
 289{
 290        struct lsm_info **lsm;
 291
 292        ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
 293                                GFP_KERNEL);
 294
 295        if (chosen_lsm_order) {
 296                if (chosen_major_lsm) {
 297                        pr_info("security= is ignored because it is superseded by lsm=\n");
 298                        chosen_major_lsm = NULL;
 299                }
 300                ordered_lsm_parse(chosen_lsm_order, "cmdline");
 301        } else
 302                ordered_lsm_parse(builtin_lsm_order, "builtin");
 303
 304        for (lsm = ordered_lsms; *lsm; lsm++)
 305                prepare_lsm(*lsm);
 306
 307        init_debug("cred blob size       = %d\n", blob_sizes.lbs_cred);
 308        init_debug("file blob size       = %d\n", blob_sizes.lbs_file);
 309        init_debug("inode blob size      = %d\n", blob_sizes.lbs_inode);
 310        init_debug("ipc blob size        = %d\n", blob_sizes.lbs_ipc);
 311        init_debug("msg_msg blob size    = %d\n", blob_sizes.lbs_msg_msg);
 312        init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
 313        init_debug("task blob size       = %d\n", blob_sizes.lbs_task);
 314
 315        /*
 316         * Create any kmem_caches needed for blobs
 317         */
 318        if (blob_sizes.lbs_file)
 319                lsm_file_cache = kmem_cache_create("lsm_file_cache",
 320                                                   blob_sizes.lbs_file, 0,
 321                                                   SLAB_PANIC, NULL);
 322        if (blob_sizes.lbs_inode)
 323                lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
 324                                                    blob_sizes.lbs_inode, 0,
 325                                                    SLAB_PANIC, NULL);
 326
 327        lsm_early_cred((struct cred *) current->cred);
 328        lsm_early_task(current);
 329        for (lsm = ordered_lsms; *lsm; lsm++)
 330                initialize_lsm(*lsm);
 331
 332        kfree(ordered_lsms);
 333}
 334
 335/**
 336 * security_init - initializes the security framework
 337 *
 338 * This should be called early in the kernel initialization sequence.
 339 */
 340int __init security_init(void)
 341{
 342        int i;
 343        struct hlist_head *list = (struct hlist_head *) &security_hook_heads;
 344
 345        pr_info("Security Framework initializing\n");
 346
 347        for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct hlist_head);
 348             i++)
 349                INIT_HLIST_HEAD(&list[i]);
 350
 351        /* Load LSMs in specified order. */
 352        ordered_lsm_init();
 353
 354        return 0;
 355}
 356
 357/* Save user chosen LSM */
 358static int __init choose_major_lsm(char *str)
 359{
 360        chosen_major_lsm = str;
 361        return 1;
 362}
 363__setup("security=", choose_major_lsm);
 364
 365/* Explicitly choose LSM initialization order. */
 366static int __init choose_lsm_order(char *str)
 367{
 368        chosen_lsm_order = str;
 369        return 1;
 370}
 371__setup("lsm=", choose_lsm_order);
 372
 373/* Enable LSM order debugging. */
 374static int __init enable_debug(char *str)
 375{
 376        debug = true;
 377        return 1;
 378}
 379__setup("lsm.debug", enable_debug);
 380
 381static bool match_last_lsm(const char *list, const char *lsm)
 382{
 383        const char *last;
 384
 385        if (WARN_ON(!list || !lsm))
 386                return false;
 387        last = strrchr(list, ',');
 388        if (last)
 389                /* Pass the comma, strcmp() will check for '\0' */
 390                last++;
 391        else
 392                last = list;
 393        return !strcmp(last, lsm);
 394}
 395
 396static int lsm_append(char *new, char **result)
 397{
 398        char *cp;
 399
 400        if (*result == NULL) {
 401                *result = kstrdup(new, GFP_KERNEL);
 402                if (*result == NULL)
 403                        return -ENOMEM;
 404        } else {
 405                /* Check if it is the last registered name */
 406                if (match_last_lsm(*result, new))
 407                        return 0;
 408                cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
 409                if (cp == NULL)
 410                        return -ENOMEM;
 411                kfree(*result);
 412                *result = cp;
 413        }
 414        return 0;
 415}
 416
 417/**
 418 * security_add_hooks - Add a modules hooks to the hook lists.
 419 * @hooks: the hooks to add
 420 * @count: the number of hooks to add
 421 * @lsm: the name of the security module
 422 *
 423 * Each LSM has to register its hooks with the infrastructure.
 424 */
 425void __init security_add_hooks(struct security_hook_list *hooks, int count,
 426                                char *lsm)
 427{
 428        int i;
 429
 430        for (i = 0; i < count; i++) {
 431                hooks[i].lsm = lsm;
 432                hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
 433        }
 434        if (lsm_append(lsm, &lsm_names) < 0)
 435                panic("%s - Cannot get early memory.\n", __func__);
 436}
 437
 438int call_blocking_lsm_notifier(enum lsm_event event, void *data)
 439{
 440        return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
 441                                            event, data);
 442}
 443EXPORT_SYMBOL(call_blocking_lsm_notifier);
 444
 445int register_blocking_lsm_notifier(struct notifier_block *nb)
 446{
 447        return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
 448                                                nb);
 449}
 450EXPORT_SYMBOL(register_blocking_lsm_notifier);
 451
 452int unregister_blocking_lsm_notifier(struct notifier_block *nb)
 453{
 454        return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
 455                                                  nb);
 456}
 457EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
 458
 459/**
 460 * lsm_cred_alloc - allocate a composite cred blob
 461 * @cred: the cred that needs a blob
 462 * @gfp: allocation type
 463 *
 464 * Allocate the cred blob for all the modules
 465 *
 466 * Returns 0, or -ENOMEM if memory can't be allocated.
 467 */
 468static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
 469{
 470        if (blob_sizes.lbs_cred == 0) {
 471                cred->security = NULL;
 472                return 0;
 473        }
 474
 475        cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
 476        if (cred->security == NULL)
 477                return -ENOMEM;
 478        return 0;
 479}
 480
 481/**
 482 * lsm_early_cred - during initialization allocate a composite cred blob
 483 * @cred: the cred that needs a blob
 484 *
 485 * Allocate the cred blob for all the modules
 486 */
 487static void __init lsm_early_cred(struct cred *cred)
 488{
 489        int rc = lsm_cred_alloc(cred, GFP_KERNEL);
 490
 491        if (rc)
 492                panic("%s: Early cred alloc failed.\n", __func__);
 493}
 494
 495/**
 496 * lsm_file_alloc - allocate a composite file blob
 497 * @file: the file that needs a blob
 498 *
 499 * Allocate the file blob for all the modules
 500 *
 501 * Returns 0, or -ENOMEM if memory can't be allocated.
 502 */
 503static int lsm_file_alloc(struct file *file)
 504{
 505        if (!lsm_file_cache) {
 506                file->f_security = NULL;
 507                return 0;
 508        }
 509
 510        file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
 511        if (file->f_security == NULL)
 512                return -ENOMEM;
 513        return 0;
 514}
 515
 516/**
 517 * lsm_inode_alloc - allocate a composite inode blob
 518 * @inode: the inode that needs a blob
 519 *
 520 * Allocate the inode blob for all the modules
 521 *
 522 * Returns 0, or -ENOMEM if memory can't be allocated.
 523 */
 524int lsm_inode_alloc(struct inode *inode)
 525{
 526        if (!lsm_inode_cache) {
 527                inode->i_security = NULL;
 528                return 0;
 529        }
 530
 531        inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
 532        if (inode->i_security == NULL)
 533                return -ENOMEM;
 534        return 0;
 535}
 536
 537/**
 538 * lsm_task_alloc - allocate a composite task blob
 539 * @task: the task that needs a blob
 540 *
 541 * Allocate the task blob for all the modules
 542 *
 543 * Returns 0, or -ENOMEM if memory can't be allocated.
 544 */
 545static int lsm_task_alloc(struct task_struct *task)
 546{
 547        if (blob_sizes.lbs_task == 0) {
 548                task->security = NULL;
 549                return 0;
 550        }
 551
 552        task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
 553        if (task->security == NULL)
 554                return -ENOMEM;
 555        return 0;
 556}
 557
 558/**
 559 * lsm_ipc_alloc - allocate a composite ipc blob
 560 * @kip: the ipc that needs a blob
 561 *
 562 * Allocate the ipc blob for all the modules
 563 *
 564 * Returns 0, or -ENOMEM if memory can't be allocated.
 565 */
 566static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
 567{
 568        if (blob_sizes.lbs_ipc == 0) {
 569                kip->security = NULL;
 570                return 0;
 571        }
 572
 573        kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
 574        if (kip->security == NULL)
 575                return -ENOMEM;
 576        return 0;
 577}
 578
 579/**
 580 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
 581 * @mp: the msg_msg that needs a blob
 582 *
 583 * Allocate the ipc blob for all the modules
 584 *
 585 * Returns 0, or -ENOMEM if memory can't be allocated.
 586 */
 587static int lsm_msg_msg_alloc(struct msg_msg *mp)
 588{
 589        if (blob_sizes.lbs_msg_msg == 0) {
 590                mp->security = NULL;
 591                return 0;
 592        }
 593
 594        mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
 595        if (mp->security == NULL)
 596                return -ENOMEM;
 597        return 0;
 598}
 599
 600/**
 601 * lsm_early_task - during initialization allocate a composite task blob
 602 * @task: the task that needs a blob
 603 *
 604 * Allocate the task blob for all the modules
 605 */
 606static void __init lsm_early_task(struct task_struct *task)
 607{
 608        int rc = lsm_task_alloc(task);
 609
 610        if (rc)
 611                panic("%s: Early task alloc failed.\n", __func__);
 612}
 613
 614/**
 615 * lsm_superblock_alloc - allocate a composite superblock blob
 616 * @sb: the superblock that needs a blob
 617 *
 618 * Allocate the superblock blob for all the modules
 619 *
 620 * Returns 0, or -ENOMEM if memory can't be allocated.
 621 */
 622static int lsm_superblock_alloc(struct super_block *sb)
 623{
 624        if (blob_sizes.lbs_superblock == 0) {
 625                sb->s_security = NULL;
 626                return 0;
 627        }
 628
 629        sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
 630        if (sb->s_security == NULL)
 631                return -ENOMEM;
 632        return 0;
 633}
 634
 635/*
 636 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
 637 * can be accessed with:
 638 *
 639 *      LSM_RET_DEFAULT(<hook_name>)
 640 *
 641 * The macros below define static constants for the default value of each
 642 * LSM hook.
 643 */
 644#define LSM_RET_DEFAULT(NAME) (NAME##_default)
 645#define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
 646#define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
 647        static const int LSM_RET_DEFAULT(NAME) = (DEFAULT);
 648#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
 649        DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
 650
 651#include <linux/lsm_hook_defs.h>
 652#undef LSM_HOOK
 653
 654/*
 655 * Hook list operation macros.
 656 *
 657 * call_void_hook:
 658 *      This is a hook that does not return a value.
 659 *
 660 * call_int_hook:
 661 *      This is a hook that returns a value.
 662 */
 663
 664#define call_void_hook(FUNC, ...)                               \
 665        do {                                                    \
 666                struct security_hook_list *P;                   \
 667                                                                \
 668                hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
 669                        P->hook.FUNC(__VA_ARGS__);              \
 670        } while (0)
 671
 672#define call_int_hook(FUNC, IRC, ...) ({                        \
 673        int RC = IRC;                                           \
 674        do {                                                    \
 675                struct security_hook_list *P;                   \
 676                                                                \
 677                hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
 678                        RC = P->hook.FUNC(__VA_ARGS__);         \
 679                        if (RC != 0)                            \
 680                                break;                          \
 681                }                                               \
 682        } while (0);                                            \
 683        RC;                                                     \
 684})
 685
 686/* Security operations */
 687
 688int security_binder_set_context_mgr(struct task_struct *mgr)
 689{
 690        return call_int_hook(binder_set_context_mgr, 0, mgr);
 691}
 692
 693int security_binder_transaction(struct task_struct *from,
 694                                struct task_struct *to)
 695{
 696        return call_int_hook(binder_transaction, 0, from, to);
 697}
 698
 699int security_binder_transfer_binder(struct task_struct *from,
 700                                    struct task_struct *to)
 701{
 702        return call_int_hook(binder_transfer_binder, 0, from, to);
 703}
 704
 705int security_binder_transfer_file(struct task_struct *from,
 706                                  struct task_struct *to, struct file *file)
 707{
 708        return call_int_hook(binder_transfer_file, 0, from, to, file);
 709}
 710
 711int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
 712{
 713        return call_int_hook(ptrace_access_check, 0, child, mode);
 714}
 715
 716int security_ptrace_traceme(struct task_struct *parent)
 717{
 718        return call_int_hook(ptrace_traceme, 0, parent);
 719}
 720
 721int security_capget(struct task_struct *target,
 722                     kernel_cap_t *effective,
 723                     kernel_cap_t *inheritable,
 724                     kernel_cap_t *permitted)
 725{
 726        return call_int_hook(capget, 0, target,
 727                                effective, inheritable, permitted);
 728}
 729
 730int security_capset(struct cred *new, const struct cred *old,
 731                    const kernel_cap_t *effective,
 732                    const kernel_cap_t *inheritable,
 733                    const kernel_cap_t *permitted)
 734{
 735        return call_int_hook(capset, 0, new, old,
 736                                effective, inheritable, permitted);
 737}
 738
 739int security_capable(const struct cred *cred,
 740                     struct user_namespace *ns,
 741                     int cap,
 742                     unsigned int opts)
 743{
 744        return call_int_hook(capable, 0, cred, ns, cap, opts);
 745}
 746
 747int security_quotactl(int cmds, int type, int id, struct super_block *sb)
 748{
 749        return call_int_hook(quotactl, 0, cmds, type, id, sb);
 750}
 751
 752int security_quota_on(struct dentry *dentry)
 753{
 754        return call_int_hook(quota_on, 0, dentry);
 755}
 756
 757int security_syslog(int type)
 758{
 759        return call_int_hook(syslog, 0, type);
 760}
 761
 762int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
 763{
 764        return call_int_hook(settime, 0, ts, tz);
 765}
 766
 767int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
 768{
 769        struct security_hook_list *hp;
 770        int cap_sys_admin = 1;
 771        int rc;
 772
 773        /*
 774         * The module will respond with a positive value if
 775         * it thinks the __vm_enough_memory() call should be
 776         * made with the cap_sys_admin set. If all of the modules
 777         * agree that it should be set it will. If any module
 778         * thinks it should not be set it won't.
 779         */
 780        hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
 781                rc = hp->hook.vm_enough_memory(mm, pages);
 782                if (rc <= 0) {
 783                        cap_sys_admin = 0;
 784                        break;
 785                }
 786        }
 787        return __vm_enough_memory(mm, pages, cap_sys_admin);
 788}
 789
 790int security_bprm_creds_for_exec(struct linux_binprm *bprm)
 791{
 792        return call_int_hook(bprm_creds_for_exec, 0, bprm);
 793}
 794
 795int security_bprm_repopulate_creds(struct linux_binprm *bprm)
 796{
 797        return call_int_hook(bprm_repopulate_creds, 0, bprm);
 798}
 799
 800int security_bprm_check(struct linux_binprm *bprm)
 801{
 802        int ret;
 803
 804        ret = call_int_hook(bprm_check_security, 0, bprm);
 805        if (ret)
 806                return ret;
 807        return ima_bprm_check(bprm);
 808}
 809
 810void security_bprm_committing_creds(struct linux_binprm *bprm)
 811{
 812        call_void_hook(bprm_committing_creds, bprm);
 813}
 814
 815void security_bprm_committed_creds(struct linux_binprm *bprm)
 816{
 817        call_void_hook(bprm_committed_creds, bprm);
 818}
 819
 820int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
 821{
 822        return call_int_hook(fs_context_dup, 0, fc, src_fc);
 823}
 824
 825int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param)
 826{
 827        return call_int_hook(fs_context_parse_param, -ENOPARAM, fc, param);
 828}
 829
 830int security_sb_alloc(struct super_block *sb)
 831{
 832        int rc = lsm_superblock_alloc(sb);
 833
 834        if (unlikely(rc))
 835                return rc;
 836        rc = call_int_hook(sb_alloc_security, 0, sb);
 837        if (unlikely(rc))
 838                security_sb_free(sb);
 839        return rc;
 840}
 841
 842void security_sb_free(struct super_block *sb)
 843{
 844        call_void_hook(sb_free_security, sb);
 845        kfree(sb->s_security);
 846        sb->s_security = NULL;
 847}
 848
 849void security_free_mnt_opts(void **mnt_opts)
 850{
 851        if (!*mnt_opts)
 852                return;
 853        call_void_hook(sb_free_mnt_opts, *mnt_opts);
 854        *mnt_opts = NULL;
 855}
 856EXPORT_SYMBOL(security_free_mnt_opts);
 857
 858int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
 859{
 860        return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
 861}
 862EXPORT_SYMBOL(security_sb_eat_lsm_opts);
 863
 864int security_sb_mnt_opts_compat(struct super_block *sb,
 865                                void *mnt_opts)
 866{
 867        return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
 868}
 869EXPORT_SYMBOL(security_sb_mnt_opts_compat);
 870
 871int security_sb_remount(struct super_block *sb,
 872                        void *mnt_opts)
 873{
 874        return call_int_hook(sb_remount, 0, sb, mnt_opts);
 875}
 876EXPORT_SYMBOL(security_sb_remount);
 877
 878int security_sb_kern_mount(struct super_block *sb)
 879{
 880        return call_int_hook(sb_kern_mount, 0, sb);
 881}
 882
 883int security_sb_show_options(struct seq_file *m, struct super_block *sb)
 884{
 885        return call_int_hook(sb_show_options, 0, m, sb);
 886}
 887
 888int security_sb_statfs(struct dentry *dentry)
 889{
 890        return call_int_hook(sb_statfs, 0, dentry);
 891}
 892
 893int security_sb_mount(const char *dev_name, const struct path *path,
 894                       const char *type, unsigned long flags, void *data)
 895{
 896        return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
 897}
 898
 899int security_sb_umount(struct vfsmount *mnt, int flags)
 900{
 901        return call_int_hook(sb_umount, 0, mnt, flags);
 902}
 903
 904int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
 905{
 906        return call_int_hook(sb_pivotroot, 0, old_path, new_path);
 907}
 908
 909int security_sb_set_mnt_opts(struct super_block *sb,
 910                                void *mnt_opts,
 911                                unsigned long kern_flags,
 912                                unsigned long *set_kern_flags)
 913{
 914        return call_int_hook(sb_set_mnt_opts,
 915                                mnt_opts ? -EOPNOTSUPP : 0, sb,
 916                                mnt_opts, kern_flags, set_kern_flags);
 917}
 918EXPORT_SYMBOL(security_sb_set_mnt_opts);
 919
 920int security_sb_clone_mnt_opts(const struct super_block *oldsb,
 921                                struct super_block *newsb,
 922                                unsigned long kern_flags,
 923                                unsigned long *set_kern_flags)
 924{
 925        return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
 926                                kern_flags, set_kern_flags);
 927}
 928EXPORT_SYMBOL(security_sb_clone_mnt_opts);
 929
 930int security_add_mnt_opt(const char *option, const char *val, int len,
 931                         void **mnt_opts)
 932{
 933        return call_int_hook(sb_add_mnt_opt, -EINVAL,
 934                                        option, val, len, mnt_opts);
 935}
 936EXPORT_SYMBOL(security_add_mnt_opt);
 937
 938int security_move_mount(const struct path *from_path, const struct path *to_path)
 939{
 940        return call_int_hook(move_mount, 0, from_path, to_path);
 941}
 942
 943int security_inode_alloc(struct inode *inode)
 944{
 945        int rc = lsm_inode_alloc(inode);
 946
 947        if (unlikely(rc))
 948                return rc;
 949        rc = call_int_hook(inode_alloc_security, 0, inode);
 950        if (unlikely(rc))
 951                security_inode_free(inode);
 952        return rc;
 953}
 954
 955static void inode_free_by_rcu(struct rcu_head *head)
 956{
 957        /*
 958         * The rcu head is at the start of the inode blob
 959         */
 960        kmem_cache_free(lsm_inode_cache, head);
 961}
 962
 963void security_inode_free(struct inode *inode)
 964{
 965        integrity_inode_free(inode);
 966        call_void_hook(inode_free_security, inode);
 967        /*
 968         * The inode may still be referenced in a path walk and
 969         * a call to security_inode_permission() can be made
 970         * after inode_free_security() is called. Ideally, the VFS
 971         * wouldn't do this, but fixing that is a much harder
 972         * job. For now, simply free the i_security via RCU, and
 973         * leave the current inode->i_security pointer intact.
 974         * The inode will be freed after the RCU grace period too.
 975         */
 976        if (inode->i_security)
 977                call_rcu((struct rcu_head *)inode->i_security,
 978                                inode_free_by_rcu);
 979}
 980
 981int security_dentry_init_security(struct dentry *dentry, int mode,
 982                                        const struct qstr *name, void **ctx,
 983                                        u32 *ctxlen)
 984{
 985        return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
 986                                name, ctx, ctxlen);
 987}
 988EXPORT_SYMBOL(security_dentry_init_security);
 989
 990int security_dentry_create_files_as(struct dentry *dentry, int mode,
 991                                    struct qstr *name,
 992                                    const struct cred *old, struct cred *new)
 993{
 994        return call_int_hook(dentry_create_files_as, 0, dentry, mode,
 995                                name, old, new);
 996}
 997EXPORT_SYMBOL(security_dentry_create_files_as);
 998
 999int security_inode_init_security(struct inode *inode, struct inode *dir,
1000                                 const struct qstr *qstr,
1001                                 const initxattrs initxattrs, void *fs_data)
1002{
1003        struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
1004        struct xattr *lsm_xattr, *evm_xattr, *xattr;
1005        int ret;
1006
1007        if (unlikely(IS_PRIVATE(inode)))
1008                return 0;
1009
1010        if (!initxattrs)
1011                return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
1012                                     dir, qstr, NULL, NULL, NULL);
1013        memset(new_xattrs, 0, sizeof(new_xattrs));
1014        lsm_xattr = new_xattrs;
1015        ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
1016                                                &lsm_xattr->name,
1017                                                &lsm_xattr->value,
1018                                                &lsm_xattr->value_len);
1019        if (ret)
1020                goto out;
1021
1022        evm_xattr = lsm_xattr + 1;
1023        ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
1024        if (ret)
1025                goto out;
1026        ret = initxattrs(inode, new_xattrs, fs_data);
1027out:
1028        for (xattr = new_xattrs; xattr->value != NULL; xattr++)
1029                kfree(xattr->value);
1030        return (ret == -EOPNOTSUPP) ? 0 : ret;
1031}
1032EXPORT_SYMBOL(security_inode_init_security);
1033
1034int security_old_inode_init_security(struct inode *inode, struct inode *dir,
1035                                     const struct qstr *qstr, const char **name,
1036                                     void **value, size_t *len)
1037{
1038        if (unlikely(IS_PRIVATE(inode)))
1039                return -EOPNOTSUPP;
1040        return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
1041                             qstr, name, value, len);
1042}
1043EXPORT_SYMBOL(security_old_inode_init_security);
1044
1045#ifdef CONFIG_SECURITY_PATH
1046int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
1047                        unsigned int dev)
1048{
1049        if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1050                return 0;
1051        return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1052}
1053EXPORT_SYMBOL(security_path_mknod);
1054
1055int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
1056{
1057        if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1058                return 0;
1059        return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1060}
1061EXPORT_SYMBOL(security_path_mkdir);
1062
1063int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1064{
1065        if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1066                return 0;
1067        return call_int_hook(path_rmdir, 0, dir, dentry);
1068}
1069
1070int security_path_unlink(const struct path *dir, struct dentry *dentry)
1071{
1072        if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1073                return 0;
1074        return call_int_hook(path_unlink, 0, dir, dentry);
1075}
1076EXPORT_SYMBOL(security_path_unlink);
1077
1078int security_path_symlink(const struct path *dir, struct dentry *dentry,
1079                          const char *old_name)
1080{
1081        if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1082                return 0;
1083        return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1084}
1085
1086int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1087                       struct dentry *new_dentry)
1088{
1089        if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1090                return 0;
1091        return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1092}
1093
1094int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1095                         const struct path *new_dir, struct dentry *new_dentry,
1096                         unsigned int flags)
1097{
1098        if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1099                     (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1100                return 0;
1101
1102        if (flags & RENAME_EXCHANGE) {
1103                int err = call_int_hook(path_rename, 0, new_dir, new_dentry,
1104                                        old_dir, old_dentry);
1105                if (err)
1106                        return err;
1107        }
1108
1109        return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1110                                new_dentry);
1111}
1112EXPORT_SYMBOL(security_path_rename);
1113
1114int security_path_truncate(const struct path *path)
1115{
1116        if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1117                return 0;
1118        return call_int_hook(path_truncate, 0, path);
1119}
1120
1121int security_path_chmod(const struct path *path, umode_t mode)
1122{
1123        if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1124                return 0;
1125        return call_int_hook(path_chmod, 0, path, mode);
1126}
1127
1128int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1129{
1130        if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1131                return 0;
1132        return call_int_hook(path_chown, 0, path, uid, gid);
1133}
1134
1135int security_path_chroot(const struct path *path)
1136{
1137        return call_int_hook(path_chroot, 0, path);
1138}
1139#endif
1140
1141int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
1142{
1143        if (unlikely(IS_PRIVATE(dir)))
1144                return 0;
1145        return call_int_hook(inode_create, 0, dir, dentry, mode);
1146}
1147EXPORT_SYMBOL_GPL(security_inode_create);
1148
1149int security_inode_link(struct dentry *old_dentry, struct inode *dir,
1150                         struct dentry *new_dentry)
1151{
1152        if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1153                return 0;
1154        return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
1155}
1156
1157int security_inode_unlink(struct inode *dir, struct dentry *dentry)
1158{
1159        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1160                return 0;
1161        return call_int_hook(inode_unlink, 0, dir, dentry);
1162}
1163
1164int security_inode_symlink(struct inode *dir, struct dentry *dentry,
1165                            const char *old_name)
1166{
1167        if (unlikely(IS_PRIVATE(dir)))
1168                return 0;
1169        return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
1170}
1171
1172int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1173{
1174        if (unlikely(IS_PRIVATE(dir)))
1175                return 0;
1176        return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
1177}
1178EXPORT_SYMBOL_GPL(security_inode_mkdir);
1179
1180int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
1181{
1182        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1183                return 0;
1184        return call_int_hook(inode_rmdir, 0, dir, dentry);
1185}
1186
1187int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1188{
1189        if (unlikely(IS_PRIVATE(dir)))
1190                return 0;
1191        return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
1192}
1193
1194int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
1195                           struct inode *new_dir, struct dentry *new_dentry,
1196                           unsigned int flags)
1197{
1198        if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1199            (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1200                return 0;
1201
1202        if (flags & RENAME_EXCHANGE) {
1203                int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
1204                                                     old_dir, old_dentry);
1205                if (err)
1206                        return err;
1207        }
1208
1209        return call_int_hook(inode_rename, 0, old_dir, old_dentry,
1210                                           new_dir, new_dentry);
1211}
1212
1213int security_inode_readlink(struct dentry *dentry)
1214{
1215        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1216                return 0;
1217        return call_int_hook(inode_readlink, 0, dentry);
1218}
1219
1220int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1221                               bool rcu)
1222{
1223        if (unlikely(IS_PRIVATE(inode)))
1224                return 0;
1225        return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
1226}
1227
1228int security_inode_permission(struct inode *inode, int mask)
1229{
1230        if (unlikely(IS_PRIVATE(inode)))
1231                return 0;
1232        return call_int_hook(inode_permission, 0, inode, mask);
1233}
1234
1235int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1236{
1237        int ret;
1238
1239        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1240                return 0;
1241        ret = call_int_hook(inode_setattr, 0, dentry, attr);
1242        if (ret)
1243                return ret;
1244        return evm_inode_setattr(dentry, attr);
1245}
1246EXPORT_SYMBOL_GPL(security_inode_setattr);
1247
1248int security_inode_getattr(const struct path *path)
1249{
1250        if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1251                return 0;
1252        return call_int_hook(inode_getattr, 0, path);
1253}
1254
1255int security_inode_setxattr(struct dentry *dentry, const char *name,
1256                            const void *value, size_t size, int flags)
1257{
1258        int ret;
1259
1260        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1261                return 0;
1262        /*
1263         * SELinux and Smack integrate the cap call,
1264         * so assume that all LSMs supplying this call do so.
1265         */
1266        ret = call_int_hook(inode_setxattr, 1, dentry, name, value, size,
1267                                flags);
1268
1269        if (ret == 1)
1270                ret = cap_inode_setxattr(dentry, name, value, size, flags);
1271        if (ret)
1272                return ret;
1273        ret = ima_inode_setxattr(dentry, name, value, size);
1274        if (ret)
1275                return ret;
1276        return evm_inode_setxattr(dentry, name, value, size);
1277}
1278
1279void security_inode_post_setxattr(struct dentry *dentry, const char *name,
1280                                  const void *value, size_t size, int flags)
1281{
1282        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1283                return;
1284        call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
1285        evm_inode_post_setxattr(dentry, name, value, size);
1286}
1287
1288int security_inode_getxattr(struct dentry *dentry, const char *name)
1289{
1290        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1291                return 0;
1292        return call_int_hook(inode_getxattr, 0, dentry, name);
1293}
1294
1295int security_inode_listxattr(struct dentry *dentry)
1296{
1297        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1298                return 0;
1299        return call_int_hook(inode_listxattr, 0, dentry);
1300}
1301
1302int security_inode_removexattr(struct dentry *dentry, const char *name)
1303{
1304        int ret;
1305
1306        if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1307                return 0;
1308        /*
1309         * SELinux and Smack integrate the cap call,
1310         * so assume that all LSMs supplying this call do so.
1311         */
1312        ret = call_int_hook(inode_removexattr, 1, dentry, name);
1313        if (ret == 1)
1314                ret = cap_inode_removexattr(dentry, name);
1315        if (ret)
1316                return ret;
1317        ret = ima_inode_removexattr(dentry, name);
1318        if (ret)
1319                return ret;
1320        return evm_inode_removexattr(dentry, name);
1321}
1322
1323int security_inode_need_killpriv(struct dentry *dentry)
1324{
1325        return call_int_hook(inode_need_killpriv, 0, dentry);
1326}
1327
1328int security_inode_killpriv(struct dentry *dentry)
1329{
1330        return call_int_hook(inode_killpriv, 0, dentry);
1331}
1332
1333int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
1334{
1335        struct security_hook_list *hp;
1336        int rc;
1337
1338        if (unlikely(IS_PRIVATE(inode)))
1339                return LSM_RET_DEFAULT(inode_getsecurity);
1340        /*
1341         * Only one module will provide an attribute with a given name.
1342         */
1343        hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
1344                rc = hp->hook.inode_getsecurity(inode, name, buffer, alloc);
1345                if (rc != LSM_RET_DEFAULT(inode_getsecurity))
1346                        return rc;
1347        }
1348        return LSM_RET_DEFAULT(inode_getsecurity);
1349}
1350
1351int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
1352{
1353        struct security_hook_list *hp;
1354        int rc;
1355
1356        if (unlikely(IS_PRIVATE(inode)))
1357                return LSM_RET_DEFAULT(inode_setsecurity);
1358        /*
1359         * Only one module will provide an attribute with a given name.
1360         */
1361        hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
1362                rc = hp->hook.inode_setsecurity(inode, name, value, size,
1363                                                                flags);
1364                if (rc != LSM_RET_DEFAULT(inode_setsecurity))
1365                        return rc;
1366        }
1367        return LSM_RET_DEFAULT(inode_setsecurity);
1368}
1369
1370int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
1371{
1372        if (unlikely(IS_PRIVATE(inode)))
1373                return 0;
1374        return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
1375}
1376EXPORT_SYMBOL(security_inode_listsecurity);
1377
1378void security_inode_getsecid(struct inode *inode, u32 *secid)
1379{
1380        call_void_hook(inode_getsecid, inode, secid);
1381}
1382
1383int security_inode_copy_up(struct dentry *src, struct cred **new)
1384{
1385        return call_int_hook(inode_copy_up, 0, src, new);
1386}
1387EXPORT_SYMBOL(security_inode_copy_up);
1388
1389int security_inode_copy_up_xattr(const char *name)
1390{
1391        struct security_hook_list *hp;
1392        int rc;
1393
1394        /*
1395         * The implementation can return 0 (accept the xattr), 1 (discard the
1396         * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
1397         * any other error code incase of an error.
1398         */
1399        hlist_for_each_entry(hp,
1400                &security_hook_heads.inode_copy_up_xattr, list) {
1401                rc = hp->hook.inode_copy_up_xattr(name);
1402                if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
1403                        return rc;
1404        }
1405
1406        return LSM_RET_DEFAULT(inode_copy_up_xattr);
1407}
1408EXPORT_SYMBOL(security_inode_copy_up_xattr);
1409
1410int security_kernfs_init_security(struct kernfs_node *kn_dir,
1411                                  struct kernfs_node *kn)
1412{
1413        return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
1414}
1415
1416int security_file_permission(struct file *file, int mask)
1417{
1418        int ret;
1419
1420        ret = call_int_hook(file_permission, 0, file, mask);
1421        if (ret)
1422                return ret;
1423
1424        return fsnotify_perm(file, mask);
1425}
1426
1427int security_file_alloc(struct file *file)
1428{
1429        int rc = lsm_file_alloc(file);
1430
1431        if (rc)
1432                return rc;
1433        rc = call_int_hook(file_alloc_security, 0, file);
1434        if (unlikely(rc))
1435                security_file_free(file);
1436        return rc;
1437}
1438
1439void security_file_free(struct file *file)
1440{
1441        void *blob;
1442
1443        call_void_hook(file_free_security, file);
1444
1445        blob = file->f_security;
1446        if (blob) {
1447                file->f_security = NULL;
1448                kmem_cache_free(lsm_file_cache, blob);
1449        }
1450}
1451
1452int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1453{
1454        return call_int_hook(file_ioctl, 0, file, cmd, arg);
1455}
1456EXPORT_SYMBOL_GPL(security_file_ioctl);
1457
1458static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
1459{
1460        /*
1461         * Does we have PROT_READ and does the application expect
1462         * it to imply PROT_EXEC?  If not, nothing to talk about...
1463         */
1464        if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
1465                return prot;
1466        if (!(current->personality & READ_IMPLIES_EXEC))
1467                return prot;
1468        /*
1469         * if that's an anonymous mapping, let it.
1470         */
1471        if (!file)
1472                return prot | PROT_EXEC;
1473        /*
1474         * ditto if it's not on noexec mount, except that on !MMU we need
1475         * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
1476         */
1477        if (!path_noexec(&file->f_path)) {
1478#ifndef CONFIG_MMU
1479                if (file->f_op->mmap_capabilities) {
1480                        unsigned caps = file->f_op->mmap_capabilities(file);
1481                        if (!(caps & NOMMU_MAP_EXEC))
1482                                return prot;
1483                }
1484#endif
1485                return prot | PROT_EXEC;
1486        }
1487        /* anything on noexec mount won't get PROT_EXEC */
1488        return prot;
1489}
1490
1491int security_mmap_file(struct file *file, unsigned long prot,
1492                        unsigned long flags)
1493{
1494        int ret;
1495        ret = call_int_hook(mmap_file, 0, file, prot,
1496                                        mmap_prot(file, prot), flags);
1497        if (ret)
1498                return ret;
1499        return ima_file_mmap(file, prot);
1500}
1501
1502int security_mmap_addr(unsigned long addr)
1503{
1504        return call_int_hook(mmap_addr, 0, addr);
1505}
1506
1507int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
1508                            unsigned long prot)
1509{
1510        return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
1511}
1512
1513int security_file_lock(struct file *file, unsigned int cmd)
1514{
1515        return call_int_hook(file_lock, 0, file, cmd);
1516}
1517
1518int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1519{
1520        return call_int_hook(file_fcntl, 0, file, cmd, arg);
1521}
1522
1523void security_file_set_fowner(struct file *file)
1524{
1525        call_void_hook(file_set_fowner, file);
1526}
1527
1528int security_file_send_sigiotask(struct task_struct *tsk,
1529                                  struct fown_struct *fown, int sig)
1530{
1531        return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
1532}
1533
1534int security_file_receive(struct file *file)
1535{
1536        return call_int_hook(file_receive, 0, file);
1537}
1538
1539int security_file_open(struct file *file)
1540{
1541        int ret;
1542
1543        ret = call_int_hook(file_open, 0, file);
1544        if (ret)
1545                return ret;
1546
1547        return fsnotify_perm(file, MAY_OPEN);
1548}
1549
1550int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
1551{
1552        int rc = lsm_task_alloc(task);
1553
1554        if (rc)
1555                return rc;
1556        rc = call_int_hook(task_alloc, 0, task, clone_flags);
1557        if (unlikely(rc))
1558                security_task_free(task);
1559        return rc;
1560}
1561
1562void security_task_free(struct task_struct *task)
1563{
1564        call_void_hook(task_free, task);
1565
1566        kfree(task->security);
1567        task->security = NULL;
1568}
1569
1570int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1571{
1572        int rc = lsm_cred_alloc(cred, gfp);
1573
1574        if (rc)
1575                return rc;
1576
1577        rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
1578        if (unlikely(rc))
1579                security_cred_free(cred);
1580        return rc;
1581}
1582
1583void security_cred_free(struct cred *cred)
1584{
1585        /*
1586         * There is a failure case in prepare_creds() that
1587         * may result in a call here with ->security being NULL.
1588         */
1589        if (unlikely(cred->security == NULL))
1590                return;
1591
1592        call_void_hook(cred_free, cred);
1593
1594        kfree(cred->security);
1595        cred->security = NULL;
1596}
1597
1598int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
1599{
1600        int rc = lsm_cred_alloc(new, gfp);
1601
1602        if (rc)
1603                return rc;
1604
1605        rc = call_int_hook(cred_prepare, 0, new, old, gfp);
1606        if (unlikely(rc))
1607                security_cred_free(new);
1608        return rc;
1609}
1610
1611void security_transfer_creds(struct cred *new, const struct cred *old)
1612{
1613        call_void_hook(cred_transfer, new, old);
1614}
1615
1616void security_cred_getsecid(const struct cred *c, u32 *secid)
1617{
1618        *secid = 0;
1619        call_void_hook(cred_getsecid, c, secid);
1620}
1621EXPORT_SYMBOL(security_cred_getsecid);
1622
1623int security_kernel_act_as(struct cred *new, u32 secid)
1624{
1625        return call_int_hook(kernel_act_as, 0, new, secid);
1626}
1627
1628int security_kernel_create_files_as(struct cred *new, struct inode *inode)
1629{
1630        return call_int_hook(kernel_create_files_as, 0, new, inode);
1631}
1632
1633int security_kernel_module_request(char *kmod_name)
1634{
1635        return call_int_hook(kernel_module_request, 0, kmod_name);
1636}
1637
1638int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
1639{
1640        int ret;
1641
1642        ret = call_int_hook(kernel_read_file, 0, file, id);
1643        if (ret)
1644                return ret;
1645        return ima_read_file(file, id);
1646}
1647EXPORT_SYMBOL_GPL(security_kernel_read_file);
1648
1649int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
1650                                   enum kernel_read_file_id id)
1651{
1652        int ret;
1653
1654        ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
1655        if (ret)
1656                return ret;
1657        return ima_post_read_file(file, buf, size, id);
1658}
1659EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
1660
1661int security_kernel_load_data(enum kernel_load_data_id id)
1662{
1663        int ret;
1664
1665        ret = call_int_hook(kernel_load_data, 0, id);
1666        if (ret)
1667                return ret;
1668        return ima_load_data(id);
1669}
1670EXPORT_SYMBOL_GPL(security_kernel_load_data);
1671
1672int security_task_fix_setuid(struct cred *new, const struct cred *old,
1673                             int flags)
1674{
1675        return call_int_hook(task_fix_setuid, 0, new, old, flags);
1676}
1677
1678int security_task_setpgid(struct task_struct *p, pid_t pgid)
1679{
1680        return call_int_hook(task_setpgid, 0, p, pgid);
1681}
1682
1683int security_task_getpgid(struct task_struct *p)
1684{
1685        return call_int_hook(task_getpgid, 0, p);
1686}
1687
1688int security_task_getsid(struct task_struct *p)
1689{
1690        return call_int_hook(task_getsid, 0, p);
1691}
1692
1693void security_task_getsecid(struct task_struct *p, u32 *secid)
1694{
1695        *secid = 0;
1696        call_void_hook(task_getsecid, p, secid);
1697}
1698EXPORT_SYMBOL(security_task_getsecid);
1699
1700int security_task_setnice(struct task_struct *p, int nice)
1701{
1702        return call_int_hook(task_setnice, 0, p, nice);
1703}
1704
1705int security_task_setioprio(struct task_struct *p, int ioprio)
1706{
1707        return call_int_hook(task_setioprio, 0, p, ioprio);
1708}
1709
1710int security_task_getioprio(struct task_struct *p)
1711{
1712        return call_int_hook(task_getioprio, 0, p);
1713}
1714
1715int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
1716                          unsigned int flags)
1717{
1718        return call_int_hook(task_prlimit, 0, cred, tcred, flags);
1719}
1720
1721int security_task_setrlimit(struct task_struct *p, unsigned int resource,
1722                struct rlimit *new_rlim)
1723{
1724        return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
1725}
1726
1727int security_task_setscheduler(struct task_struct *p)
1728{
1729        return call_int_hook(task_setscheduler, 0, p);
1730}
1731
1732int security_task_getscheduler(struct task_struct *p)
1733{
1734        return call_int_hook(task_getscheduler, 0, p);
1735}
1736
1737int security_task_movememory(struct task_struct *p)
1738{
1739        return call_int_hook(task_movememory, 0, p);
1740}
1741
1742int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
1743                        int sig, const struct cred *cred)
1744{
1745        return call_int_hook(task_kill, 0, p, info, sig, cred);
1746}
1747
1748int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1749                         unsigned long arg4, unsigned long arg5)
1750{
1751        int thisrc;
1752        int rc = LSM_RET_DEFAULT(task_prctl);
1753        struct security_hook_list *hp;
1754
1755        hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
1756                thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
1757                if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
1758                        rc = thisrc;
1759                        if (thisrc != 0)
1760                                break;
1761                }
1762        }
1763        return rc;
1764}
1765
1766void security_task_to_inode(struct task_struct *p, struct inode *inode)
1767{
1768        call_void_hook(task_to_inode, p, inode);
1769}
1770
1771int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
1772{
1773        return call_int_hook(ipc_permission, 0, ipcp, flag);
1774}
1775
1776void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
1777{
1778        *secid = 0;
1779        call_void_hook(ipc_getsecid, ipcp, secid);
1780}
1781
1782int security_msg_msg_alloc(struct msg_msg *msg)
1783{
1784        int rc = lsm_msg_msg_alloc(msg);
1785
1786        if (unlikely(rc))
1787                return rc;
1788        rc = call_int_hook(msg_msg_alloc_security, 0, msg);
1789        if (unlikely(rc))
1790                security_msg_msg_free(msg);
1791        return rc;
1792}
1793
1794void security_msg_msg_free(struct msg_msg *msg)
1795{
1796        call_void_hook(msg_msg_free_security, msg);
1797        kfree(msg->security);
1798        msg->security = NULL;
1799}
1800
1801int security_msg_queue_alloc(struct kern_ipc_perm *msq)
1802{
1803        int rc = lsm_ipc_alloc(msq);
1804
1805        if (unlikely(rc))
1806                return rc;
1807        rc = call_int_hook(msg_queue_alloc_security, 0, msq);
1808        if (unlikely(rc))
1809                security_msg_queue_free(msq);
1810        return rc;
1811}
1812
1813void security_msg_queue_free(struct kern_ipc_perm *msq)
1814{
1815        call_void_hook(msg_queue_free_security, msq);
1816        kfree(msq->security);
1817        msq->security = NULL;
1818}
1819
1820int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
1821{
1822        return call_int_hook(msg_queue_associate, 0, msq, msqflg);
1823}
1824
1825int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
1826{
1827        return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
1828}
1829
1830int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
1831                               struct msg_msg *msg, int msqflg)
1832{
1833        return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
1834}
1835
1836int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
1837                               struct task_struct *target, long type, int mode)
1838{
1839        return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
1840}
1841
1842int security_shm_alloc(struct kern_ipc_perm *shp)
1843{
1844        int rc = lsm_ipc_alloc(shp);
1845
1846        if (unlikely(rc))
1847                return rc;
1848        rc = call_int_hook(shm_alloc_security, 0, shp);
1849        if (unlikely(rc))
1850                security_shm_free(shp);
1851        return rc;
1852}
1853
1854void security_shm_free(struct kern_ipc_perm *shp)
1855{
1856        call_void_hook(shm_free_security, shp);
1857        kfree(shp->security);
1858        shp->security = NULL;
1859}
1860
1861int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
1862{
1863        return call_int_hook(shm_associate, 0, shp, shmflg);
1864}
1865
1866int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
1867{
1868        return call_int_hook(shm_shmctl, 0, shp, cmd);
1869}
1870
1871int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmflg)
1872{
1873        return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
1874}
1875
1876int security_sem_alloc(struct kern_ipc_perm *sma)
1877{
1878        int rc = lsm_ipc_alloc(sma);
1879
1880        if (unlikely(rc))
1881                return rc;
1882        rc = call_int_hook(sem_alloc_security, 0, sma);
1883        if (unlikely(rc))
1884                security_sem_free(sma);
1885        return rc;
1886}
1887
1888void security_sem_free(struct kern_ipc_perm *sma)
1889{
1890        call_void_hook(sem_free_security, sma);
1891        kfree(sma->security);
1892        sma->security = NULL;
1893}
1894
1895int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
1896{
1897        return call_int_hook(sem_associate, 0, sma, semflg);
1898}
1899
1900int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
1901{
1902        return call_int_hook(sem_semctl, 0, sma, cmd);
1903}
1904
1905int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
1906                        unsigned nsops, int alter)
1907{
1908        return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
1909}
1910
1911void security_d_instantiate(struct dentry *dentry, struct inode *inode)
1912{
1913        if (unlikely(inode && IS_PRIVATE(inode)))
1914                return;
1915        call_void_hook(d_instantiate, dentry, inode);
1916}
1917EXPORT_SYMBOL(security_d_instantiate);
1918
1919int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
1920                                char **value)
1921{
1922        struct security_hook_list *hp;
1923
1924        hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
1925                if (lsm != NULL && strcmp(lsm, hp->lsm))
1926                        continue;
1927                return hp->hook.getprocattr(p, name, value);
1928        }
1929        return LSM_RET_DEFAULT(getprocattr);
1930}
1931
1932int security_setprocattr(const char *lsm, const char *name, void *value,
1933                         size_t size)
1934{
1935        struct security_hook_list *hp;
1936
1937        hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
1938                if (lsm != NULL && strcmp(lsm, hp->lsm))
1939                        continue;
1940                return hp->hook.setprocattr(name, value, size);
1941        }
1942        return LSM_RET_DEFAULT(setprocattr);
1943}
1944
1945int security_netlink_send(struct sock *sk, struct sk_buff *skb)
1946{
1947        return call_int_hook(netlink_send, 0, sk, skb);
1948}
1949
1950int security_ismaclabel(const char *name)
1951{
1952        return call_int_hook(ismaclabel, 0, name);
1953}
1954EXPORT_SYMBOL(security_ismaclabel);
1955
1956int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
1957{
1958        struct security_hook_list *hp;
1959        int rc;
1960
1961        /*
1962         * Currently, only one LSM can implement secid_to_secctx (i.e this
1963         * LSM hook is not "stackable").
1964         */
1965        hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
1966                rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
1967                if (rc != LSM_RET_DEFAULT(secid_to_secctx))
1968                        return rc;
1969        }
1970
1971        return LSM_RET_DEFAULT(secid_to_secctx);
1972}
1973EXPORT_SYMBOL(security_secid_to_secctx);
1974
1975int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
1976{
1977        *secid = 0;
1978        return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
1979}
1980EXPORT_SYMBOL(security_secctx_to_secid);
1981
1982void security_release_secctx(char *secdata, u32 seclen)
1983{
1984        call_void_hook(release_secctx, secdata, seclen);
1985}
1986EXPORT_SYMBOL(security_release_secctx);
1987
1988void security_inode_invalidate_secctx(struct inode *inode)
1989{
1990        call_void_hook(inode_invalidate_secctx, inode);
1991}
1992EXPORT_SYMBOL(security_inode_invalidate_secctx);
1993
1994int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
1995{
1996        return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
1997}
1998EXPORT_SYMBOL(security_inode_notifysecctx);
1999
2000int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
2001{
2002        return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
2003}
2004EXPORT_SYMBOL(security_inode_setsecctx);
2005
2006int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
2007{
2008        return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
2009}
2010EXPORT_SYMBOL(security_inode_getsecctx);
2011
2012#ifdef CONFIG_SECURITY_NETWORK
2013
2014int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
2015{
2016        return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
2017}
2018EXPORT_SYMBOL(security_unix_stream_connect);
2019
2020int security_unix_may_send(struct socket *sock,  struct socket *other)
2021{
2022        return call_int_hook(unix_may_send, 0, sock, other);
2023}
2024EXPORT_SYMBOL(security_unix_may_send);
2025
2026int security_socket_create(int family, int type, int protocol, int kern)
2027{
2028        return call_int_hook(socket_create, 0, family, type, protocol, kern);
2029}
2030
2031int security_socket_post_create(struct socket *sock, int family,
2032                                int type, int protocol, int kern)
2033{
2034        return call_int_hook(socket_post_create, 0, sock, family, type,
2035                                                protocol, kern);
2036}
2037
2038int security_socket_socketpair(struct socket *socka, struct socket *sockb)
2039{
2040        return call_int_hook(socket_socketpair, 0, socka, sockb);
2041}
2042EXPORT_SYMBOL(security_socket_socketpair);
2043
2044int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
2045{
2046        return call_int_hook(socket_bind, 0, sock, address, addrlen);
2047}
2048
2049int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
2050{
2051        return call_int_hook(socket_connect, 0, sock, address, addrlen);
2052}
2053
2054int security_socket_listen(struct socket *sock, int backlog)
2055{
2056        return call_int_hook(socket_listen, 0, sock, backlog);
2057}
2058
2059int security_socket_accept(struct socket *sock, struct socket *newsock)
2060{
2061        return call_int_hook(socket_accept, 0, sock, newsock);
2062}
2063
2064int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
2065{
2066        return call_int_hook(socket_sendmsg, 0, sock, msg, size);
2067}
2068
2069int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
2070                            int size, int flags)
2071{
2072        return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
2073}
2074
2075int security_socket_getsockname(struct socket *sock)
2076{
2077        return call_int_hook(socket_getsockname, 0, sock);
2078}
2079
2080int security_socket_getpeername(struct socket *sock)
2081{
2082        return call_int_hook(socket_getpeername, 0, sock);
2083}
2084
2085int security_socket_getsockopt(struct socket *sock, int level, int optname)
2086{
2087        return call_int_hook(socket_getsockopt, 0, sock, level, optname);
2088}
2089
2090int security_socket_setsockopt(struct socket *sock, int level, int optname)
2091{
2092        return call_int_hook(socket_setsockopt, 0, sock, level, optname);
2093}
2094
2095int security_socket_shutdown(struct socket *sock, int how)
2096{
2097        return call_int_hook(socket_shutdown, 0, sock, how);
2098}
2099
2100int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2101{
2102        return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
2103}
2104EXPORT_SYMBOL(security_sock_rcv_skb);
2105
2106int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
2107                                      int __user *optlen, unsigned len)
2108{
2109        return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
2110                                optval, optlen, len);
2111}
2112
2113int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
2114{
2115        return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
2116                             skb, secid);
2117}
2118EXPORT_SYMBOL(security_socket_getpeersec_dgram);
2119
2120int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
2121{
2122        return call_int_hook(sk_alloc_security, 0, sk, family, priority);
2123}
2124
2125void security_sk_free(struct sock *sk)
2126{
2127        call_void_hook(sk_free_security, sk);
2128}
2129
2130void security_sk_clone(const struct sock *sk, struct sock *newsk)
2131{
2132        call_void_hook(sk_clone_security, sk, newsk);
2133}
2134EXPORT_SYMBOL(security_sk_clone);
2135
2136void security_sk_classify_flow(struct sock *sk, struct flowi *fl)
2137{
2138        call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
2139}
2140EXPORT_SYMBOL(security_sk_classify_flow);
2141
2142void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)
2143{
2144        call_void_hook(req_classify_flow, req, fl);
2145}
2146EXPORT_SYMBOL(security_req_classify_flow);
2147
2148void security_sock_graft(struct sock *sk, struct socket *parent)
2149{
2150        call_void_hook(sock_graft, sk, parent);
2151}
2152EXPORT_SYMBOL(security_sock_graft);
2153
2154int security_inet_conn_request(const struct sock *sk,
2155                        struct sk_buff *skb, struct request_sock *req)
2156{
2157        return call_int_hook(inet_conn_request, 0, sk, skb, req);
2158}
2159EXPORT_SYMBOL(security_inet_conn_request);
2160
2161void security_inet_csk_clone(struct sock *newsk,
2162                        const struct request_sock *req)
2163{
2164        call_void_hook(inet_csk_clone, newsk, req);
2165}
2166
2167void security_inet_conn_established(struct sock *sk,
2168                        struct sk_buff *skb)
2169{
2170        call_void_hook(inet_conn_established, sk, skb);
2171}
2172EXPORT_SYMBOL(security_inet_conn_established);
2173
2174int security_secmark_relabel_packet(u32 secid)
2175{
2176        return call_int_hook(secmark_relabel_packet, 0, secid);
2177}
2178EXPORT_SYMBOL(security_secmark_relabel_packet);
2179
2180void security_secmark_refcount_inc(void)
2181{
2182        call_void_hook(secmark_refcount_inc);
2183}
2184EXPORT_SYMBOL(security_secmark_refcount_inc);
2185
2186void security_secmark_refcount_dec(void)
2187{
2188        call_void_hook(secmark_refcount_dec);
2189}
2190EXPORT_SYMBOL(security_secmark_refcount_dec);
2191
2192int security_tun_dev_alloc_security(void **security)
2193{
2194        return call_int_hook(tun_dev_alloc_security, 0, security);
2195}
2196EXPORT_SYMBOL(security_tun_dev_alloc_security);
2197
2198void security_tun_dev_free_security(void *security)
2199{
2200        call_void_hook(tun_dev_free_security, security);
2201}
2202EXPORT_SYMBOL(security_tun_dev_free_security);
2203
2204int security_tun_dev_create(void)
2205{
2206        return call_int_hook(tun_dev_create, 0);
2207}
2208EXPORT_SYMBOL(security_tun_dev_create);
2209
2210int security_tun_dev_attach_queue(void *security)
2211{
2212        return call_int_hook(tun_dev_attach_queue, 0, security);
2213}
2214EXPORT_SYMBOL(security_tun_dev_attach_queue);
2215
2216int security_tun_dev_attach(struct sock *sk, void *security)
2217{
2218        return call_int_hook(tun_dev_attach, 0, sk, security);
2219}
2220EXPORT_SYMBOL(security_tun_dev_attach);
2221
2222int security_tun_dev_open(void *security)
2223{
2224        return call_int_hook(tun_dev_open, 0, security);
2225}
2226EXPORT_SYMBOL(security_tun_dev_open);
2227
2228int security_sctp_assoc_request(struct sctp_association *asoc, struct sk_buff *skb)
2229{
2230        return call_int_hook(sctp_assoc_request, 0, asoc, skb);
2231}
2232EXPORT_SYMBOL(security_sctp_assoc_request);
2233
2234int security_sctp_bind_connect(struct sock *sk, int optname,
2235                               struct sockaddr *address, int addrlen)
2236{
2237        return call_int_hook(sctp_bind_connect, 0, sk, optname,
2238                             address, addrlen);
2239}
2240EXPORT_SYMBOL(security_sctp_bind_connect);
2241
2242void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
2243                            struct sock *newsk)
2244{
2245        call_void_hook(sctp_sk_clone, asoc, sk, newsk);
2246}
2247EXPORT_SYMBOL(security_sctp_sk_clone);
2248
2249int security_sctp_assoc_established(struct sctp_association *asoc,
2250                                    struct sk_buff *skb)
2251{
2252        return call_int_hook(sctp_assoc_established, 0, asoc, skb);
2253}
2254EXPORT_SYMBOL(security_sctp_assoc_established);
2255
2256#endif  /* CONFIG_SECURITY_NETWORK */
2257
2258#ifdef CONFIG_SECURITY_INFINIBAND
2259
2260int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
2261{
2262        return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
2263}
2264EXPORT_SYMBOL(security_ib_pkey_access);
2265
2266int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
2267{
2268        return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
2269}
2270EXPORT_SYMBOL(security_ib_endport_manage_subnet);
2271
2272int security_ib_alloc_security(void **sec)
2273{
2274        return call_int_hook(ib_alloc_security, 0, sec);
2275}
2276EXPORT_SYMBOL(security_ib_alloc_security);
2277
2278void security_ib_free_security(void *sec)
2279{
2280        call_void_hook(ib_free_security, sec);
2281}
2282EXPORT_SYMBOL(security_ib_free_security);
2283#endif  /* CONFIG_SECURITY_INFINIBAND */
2284
2285#ifdef CONFIG_SECURITY_NETWORK_XFRM
2286
2287int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
2288                               struct xfrm_user_sec_ctx *sec_ctx,
2289                               gfp_t gfp)
2290{
2291        return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
2292}
2293EXPORT_SYMBOL(security_xfrm_policy_alloc);
2294
2295int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
2296                              struct xfrm_sec_ctx **new_ctxp)
2297{
2298        return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
2299}
2300
2301void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
2302{
2303        call_void_hook(xfrm_policy_free_security, ctx);
2304}
2305EXPORT_SYMBOL(security_xfrm_policy_free);
2306
2307int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
2308{
2309        return call_int_hook(xfrm_policy_delete_security, 0, ctx);
2310}
2311
2312int security_xfrm_state_alloc(struct xfrm_state *x,
2313                              struct xfrm_user_sec_ctx *sec_ctx)
2314{
2315        return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
2316}
2317EXPORT_SYMBOL(security_xfrm_state_alloc);
2318
2319int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
2320                                      struct xfrm_sec_ctx *polsec, u32 secid)
2321{
2322        return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
2323}
2324
2325int security_xfrm_state_delete(struct xfrm_state *x)
2326{
2327        return call_int_hook(xfrm_state_delete_security, 0, x);
2328}
2329EXPORT_SYMBOL(security_xfrm_state_delete);
2330
2331void security_xfrm_state_free(struct xfrm_state *x)
2332{
2333        call_void_hook(xfrm_state_free_security, x);
2334}
2335
2336int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
2337{
2338        return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid, dir);
2339}
2340
2341int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
2342                                       struct xfrm_policy *xp,
2343                                       const struct flowi *fl)
2344{
2345        struct security_hook_list *hp;
2346        int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
2347
2348        /*
2349         * Since this function is expected to return 0 or 1, the judgment
2350         * becomes difficult if multiple LSMs supply this call. Fortunately,
2351         * we can use the first LSM's judgment because currently only SELinux
2352         * supplies this call.
2353         *
2354         * For speed optimization, we explicitly break the loop rather than
2355         * using the macro
2356         */
2357        hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
2358                                list) {
2359                rc = hp->hook.xfrm_state_pol_flow_match(x, xp, fl);
2360                break;
2361        }
2362        return rc;
2363}
2364
2365int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
2366{
2367        return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
2368}
2369
2370void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
2371{
2372        int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl->flowi_secid,
2373                                0);
2374
2375        BUG_ON(rc);
2376}
2377EXPORT_SYMBOL(security_skb_classify_flow);
2378
2379#endif  /* CONFIG_SECURITY_NETWORK_XFRM */
2380
2381#ifdef CONFIG_KEYS
2382
2383int security_key_alloc(struct key *key, const struct cred *cred,
2384                       unsigned long flags)
2385{
2386        return call_int_hook(key_alloc, 0, key, cred, flags);
2387}
2388
2389void security_key_free(struct key *key)
2390{
2391        call_void_hook(key_free, key);
2392}
2393
2394int security_key_permission(key_ref_t key_ref,
2395                            const struct cred *cred, unsigned perm)
2396{
2397        return call_int_hook(key_permission, 0, key_ref, cred, perm);
2398}
2399
2400int security_key_getsecurity(struct key *key, char **_buffer)
2401{
2402        *_buffer = NULL;
2403        return call_int_hook(key_getsecurity, 0, key, _buffer);
2404}
2405
2406#endif  /* CONFIG_KEYS */
2407
2408#ifdef CONFIG_AUDIT
2409
2410int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2411{
2412        return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
2413}
2414
2415int security_audit_rule_known(struct audit_krule *krule)
2416{
2417        return call_int_hook(audit_rule_known, 0, krule);
2418}
2419
2420void security_audit_rule_free(void *lsmrule)
2421{
2422        call_void_hook(audit_rule_free, lsmrule);
2423}
2424
2425int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
2426{
2427        return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
2428}
2429#endif /* CONFIG_AUDIT */
2430
2431#ifdef CONFIG_BPF_SYSCALL
2432int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
2433{
2434        return call_int_hook(bpf, 0, cmd, attr, size);
2435}
2436int security_bpf_map(struct bpf_map *map, fmode_t fmode)
2437{
2438        return call_int_hook(bpf_map, 0, map, fmode);
2439}
2440int security_bpf_prog(struct bpf_prog *prog)
2441{
2442        return call_int_hook(bpf_prog, 0, prog);
2443}
2444int security_bpf_map_alloc(struct bpf_map *map)
2445{
2446        return call_int_hook(bpf_map_alloc_security, 0, map);
2447}
2448int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
2449{
2450        return call_int_hook(bpf_prog_alloc_security, 0, aux);
2451}
2452void security_bpf_map_free(struct bpf_map *map)
2453{
2454        call_void_hook(bpf_map_free_security, map);
2455}
2456void security_bpf_prog_free(struct bpf_prog_aux *aux)
2457{
2458        call_void_hook(bpf_prog_free_security, aux);
2459}
2460#endif /* CONFIG_BPF_SYSCALL */
2461
2462#ifdef CONFIG_PERF_EVENTS
2463int security_perf_event_open(struct perf_event_attr *attr, int type)
2464{
2465        return call_int_hook(perf_event_open, 0, attr, type);
2466}
2467
2468int security_perf_event_alloc(struct perf_event *event)
2469{
2470        return call_int_hook(perf_event_alloc, 0, event);
2471}
2472
2473void security_perf_event_free(struct perf_event *event)
2474{
2475        call_void_hook(perf_event_free, event);
2476}
2477
2478int security_perf_event_read(struct perf_event *event)
2479{
2480        return call_int_hook(perf_event_read, 0, event);
2481}
2482
2483int security_perf_event_write(struct perf_event *event)
2484{
2485        return call_int_hook(perf_event_write, 0, event);
2486}
2487#endif /* CONFIG_PERF_EVENTS */
2488