/*1* Simplified MAC Kernel (smack) security module2*3* This file contains the smack hook function implementations.4*5* Authors:6* Casey Schaufler <[email protected]>7* Jarkko Sakkinen <[email protected]>8*9* Copyright (C) 2007 Casey Schaufler <[email protected]>10* Copyright (C) 2009 Hewlett-Packard Development Company, L.P.11* Paul Moore <[email protected]>12* Copyright (C) 2010 Nokia Corporation13*14* This program is free software; you can redistribute it and/or modify15* it under the terms of the GNU General Public License version 2,16* as published by the Free Software Foundation.17*/1819#include <linux/xattr.h>20#include <linux/pagemap.h>21#include <linux/mount.h>22#include <linux/stat.h>23#include <linux/kd.h>24#include <asm/ioctls.h>25#include <linux/ip.h>26#include <linux/tcp.h>27#include <linux/udp.h>28#include <linux/slab.h>29#include <linux/mutex.h>30#include <linux/pipe_fs_i.h>31#include <net/netlabel.h>32#include <net/cipso_ipv4.h>33#include <linux/audit.h>34#include <linux/magic.h>35#include <linux/dcache.h>36#include "smack.h"3738#define task_security(task) (task_cred_xxx((task), security))3940#define TRANS_TRUE "TRUE"41#define TRANS_TRUE_SIZE 44243/**44* smk_fetch - Fetch the smack label from a file.45* @ip: a pointer to the inode46* @dp: a pointer to the dentry47*48* Returns a pointer to the master list entry for the Smack label49* or NULL if there was no label to fetch.50*/51static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)52{53int rc;54char in[SMK_LABELLEN];5556if (ip->i_op->getxattr == NULL)57return NULL;5859rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);60if (rc < 0)61return NULL;6263return smk_import(in, rc);64}6566/**67* new_inode_smack - allocate an inode security blob68* @smack: a pointer to the Smack label to use in the blob69*70* Returns the new blob or NULL if there's no memory available71*/72struct inode_smack *new_inode_smack(char *smack)73{74struct inode_smack *isp;7576isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);77if (isp == NULL)78return NULL;7980isp->smk_inode = smack;81isp->smk_flags = 0;82mutex_init(&isp->smk_lock);8384return isp;85}8687/**88* new_task_smack - allocate a task security blob89* @smack: a pointer to the Smack label to use in the blob90*91* Returns the new blob or NULL if there's no memory available92*/93static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)94{95struct task_smack *tsp;9697tsp = kzalloc(sizeof(struct task_smack), gfp);98if (tsp == NULL)99return NULL;100101tsp->smk_task = task;102tsp->smk_forked = forked;103INIT_LIST_HEAD(&tsp->smk_rules);104mutex_init(&tsp->smk_rules_lock);105106return tsp;107}108109/**110* smk_copy_rules - copy a rule set111* @nhead - new rules header pointer112* @ohead - old rules header pointer113*114* Returns 0 on success, -ENOMEM on error115*/116static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,117gfp_t gfp)118{119struct smack_rule *nrp;120struct smack_rule *orp;121int rc = 0;122123INIT_LIST_HEAD(nhead);124125list_for_each_entry_rcu(orp, ohead, list) {126nrp = kzalloc(sizeof(struct smack_rule), gfp);127if (nrp == NULL) {128rc = -ENOMEM;129break;130}131*nrp = *orp;132list_add_rcu(&nrp->list, nhead);133}134return rc;135}136137/*138* LSM hooks.139* We he, that is fun!140*/141142/**143* smack_ptrace_access_check - Smack approval on PTRACE_ATTACH144* @ctp: child task pointer145* @mode: ptrace attachment mode146*147* Returns 0 if access is OK, an error code otherwise148*149* Do the capability checks, and require read and write.150*/151static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)152{153int rc;154struct smk_audit_info ad;155char *tsp;156157rc = cap_ptrace_access_check(ctp, mode);158if (rc != 0)159return rc;160161tsp = smk_of_task(task_security(ctp));162smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);163smk_ad_setfield_u_tsk(&ad, ctp);164165rc = smk_curacc(tsp, MAY_READWRITE, &ad);166return rc;167}168169/**170* smack_ptrace_traceme - Smack approval on PTRACE_TRACEME171* @ptp: parent task pointer172*173* Returns 0 if access is OK, an error code otherwise174*175* Do the capability checks, and require read and write.176*/177static int smack_ptrace_traceme(struct task_struct *ptp)178{179int rc;180struct smk_audit_info ad;181char *tsp;182183rc = cap_ptrace_traceme(ptp);184if (rc != 0)185return rc;186187tsp = smk_of_task(task_security(ptp));188smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);189smk_ad_setfield_u_tsk(&ad, ptp);190191rc = smk_curacc(tsp, MAY_READWRITE, &ad);192return rc;193}194195/**196* smack_syslog - Smack approval on syslog197* @type: message type198*199* Require that the task has the floor label200*201* Returns 0 on success, error code otherwise.202*/203static int smack_syslog(int typefrom_file)204{205int rc = 0;206char *sp = smk_of_current();207208if (capable(CAP_MAC_OVERRIDE))209return 0;210211if (sp != smack_known_floor.smk_known)212rc = -EACCES;213214return rc;215}216217218/*219* Superblock Hooks.220*/221222/**223* smack_sb_alloc_security - allocate a superblock blob224* @sb: the superblock getting the blob225*226* Returns 0 on success or -ENOMEM on error.227*/228static int smack_sb_alloc_security(struct super_block *sb)229{230struct superblock_smack *sbsp;231232sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);233234if (sbsp == NULL)235return -ENOMEM;236237sbsp->smk_root = smack_known_floor.smk_known;238sbsp->smk_default = smack_known_floor.smk_known;239sbsp->smk_floor = smack_known_floor.smk_known;240sbsp->smk_hat = smack_known_hat.smk_known;241sbsp->smk_initialized = 0;242spin_lock_init(&sbsp->smk_sblock);243244sb->s_security = sbsp;245246return 0;247}248249/**250* smack_sb_free_security - free a superblock blob251* @sb: the superblock getting the blob252*253*/254static void smack_sb_free_security(struct super_block *sb)255{256kfree(sb->s_security);257sb->s_security = NULL;258}259260/**261* smack_sb_copy_data - copy mount options data for processing262* @orig: where to start263* @smackopts: mount options string264*265* Returns 0 on success or -ENOMEM on error.266*267* Copy the Smack specific mount options out of the mount268* options list.269*/270static int smack_sb_copy_data(char *orig, char *smackopts)271{272char *cp, *commap, *otheropts, *dp;273274otheropts = (char *)get_zeroed_page(GFP_KERNEL);275if (otheropts == NULL)276return -ENOMEM;277278for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {279if (strstr(cp, SMK_FSDEFAULT) == cp)280dp = smackopts;281else if (strstr(cp, SMK_FSFLOOR) == cp)282dp = smackopts;283else if (strstr(cp, SMK_FSHAT) == cp)284dp = smackopts;285else if (strstr(cp, SMK_FSROOT) == cp)286dp = smackopts;287else288dp = otheropts;289290commap = strchr(cp, ',');291if (commap != NULL)292*commap = '\0';293294if (*dp != '\0')295strcat(dp, ",");296strcat(dp, cp);297}298299strcpy(orig, otheropts);300free_page((unsigned long)otheropts);301302return 0;303}304305/**306* smack_sb_kern_mount - Smack specific mount processing307* @sb: the file system superblock308* @flags: the mount flags309* @data: the smack mount options310*311* Returns 0 on success, an error code on failure312*/313static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)314{315struct dentry *root = sb->s_root;316struct inode *inode = root->d_inode;317struct superblock_smack *sp = sb->s_security;318struct inode_smack *isp;319char *op;320char *commap;321char *nsp;322323spin_lock(&sp->smk_sblock);324if (sp->smk_initialized != 0) {325spin_unlock(&sp->smk_sblock);326return 0;327}328sp->smk_initialized = 1;329spin_unlock(&sp->smk_sblock);330331for (op = data; op != NULL; op = commap) {332commap = strchr(op, ',');333if (commap != NULL)334*commap++ = '\0';335336if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {337op += strlen(SMK_FSHAT);338nsp = smk_import(op, 0);339if (nsp != NULL)340sp->smk_hat = nsp;341} else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {342op += strlen(SMK_FSFLOOR);343nsp = smk_import(op, 0);344if (nsp != NULL)345sp->smk_floor = nsp;346} else if (strncmp(op, SMK_FSDEFAULT,347strlen(SMK_FSDEFAULT)) == 0) {348op += strlen(SMK_FSDEFAULT);349nsp = smk_import(op, 0);350if (nsp != NULL)351sp->smk_default = nsp;352} else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {353op += strlen(SMK_FSROOT);354nsp = smk_import(op, 0);355if (nsp != NULL)356sp->smk_root = nsp;357}358}359360/*361* Initialize the root inode.362*/363isp = inode->i_security;364if (isp == NULL)365inode->i_security = new_inode_smack(sp->smk_root);366else367isp->smk_inode = sp->smk_root;368369return 0;370}371372/**373* smack_sb_statfs - Smack check on statfs374* @dentry: identifies the file system in question375*376* Returns 0 if current can read the floor of the filesystem,377* and error code otherwise378*/379static int smack_sb_statfs(struct dentry *dentry)380{381struct superblock_smack *sbp = dentry->d_sb->s_security;382int rc;383struct smk_audit_info ad;384385smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);386smk_ad_setfield_u_fs_path_dentry(&ad, dentry);387388rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);389return rc;390}391392/**393* smack_sb_mount - Smack check for mounting394* @dev_name: unused395* @path: mount point396* @type: unused397* @flags: unused398* @data: unused399*400* Returns 0 if current can write the floor of the filesystem401* being mounted on, an error code otherwise.402*/403static int smack_sb_mount(char *dev_name, struct path *path,404char *type, unsigned long flags, void *data)405{406struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;407struct smk_audit_info ad;408409smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);410smk_ad_setfield_u_fs_path(&ad, *path);411412return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);413}414415/**416* smack_sb_umount - Smack check for unmounting417* @mnt: file system to unmount418* @flags: unused419*420* Returns 0 if current can write the floor of the filesystem421* being unmounted, an error code otherwise.422*/423static int smack_sb_umount(struct vfsmount *mnt, int flags)424{425struct superblock_smack *sbp;426struct smk_audit_info ad;427struct path path;428429path.dentry = mnt->mnt_root;430path.mnt = mnt;431432smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);433smk_ad_setfield_u_fs_path(&ad, path);434435sbp = mnt->mnt_sb->s_security;436return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);437}438439/*440* BPRM hooks441*/442443static int smack_bprm_set_creds(struct linux_binprm *bprm)444{445struct task_smack *tsp = bprm->cred->security;446struct inode_smack *isp;447struct dentry *dp;448int rc;449450rc = cap_bprm_set_creds(bprm);451if (rc != 0)452return rc;453454if (bprm->cred_prepared)455return 0;456457if (bprm->file == NULL || bprm->file->f_dentry == NULL)458return 0;459460dp = bprm->file->f_dentry;461462if (dp->d_inode == NULL)463return 0;464465isp = dp->d_inode->i_security;466467if (isp->smk_task != NULL)468tsp->smk_task = isp->smk_task;469470return 0;471}472473/*474* Inode hooks475*/476477/**478* smack_inode_alloc_security - allocate an inode blob479* @inode: the inode in need of a blob480*481* Returns 0 if it gets a blob, -ENOMEM otherwise482*/483static int smack_inode_alloc_security(struct inode *inode)484{485inode->i_security = new_inode_smack(smk_of_current());486if (inode->i_security == NULL)487return -ENOMEM;488return 0;489}490491/**492* smack_inode_free_security - free an inode blob493* @inode: the inode with a blob494*495* Clears the blob pointer in inode496*/497static void smack_inode_free_security(struct inode *inode)498{499kfree(inode->i_security);500inode->i_security = NULL;501}502503/**504* smack_inode_init_security - copy out the smack from an inode505* @inode: the inode506* @dir: unused507* @qstr: unused508* @name: where to put the attribute name509* @value: where to put the attribute value510* @len: where to put the length of the attribute511*512* Returns 0 if it all works out, -ENOMEM if there's no memory513*/514static int smack_inode_init_security(struct inode *inode, struct inode *dir,515const struct qstr *qstr, char **name,516void **value, size_t *len)517{518char *isp = smk_of_inode(inode);519char *dsp = smk_of_inode(dir);520int may;521522if (name) {523*name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);524if (*name == NULL)525return -ENOMEM;526}527528if (value) {529rcu_read_lock();530may = smk_access_entry(smk_of_current(), dsp, &smack_rule_list);531rcu_read_unlock();532533/*534* If the access rule allows transmutation and535* the directory requests transmutation then536* by all means transmute.537*/538if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&539smk_inode_transmutable(dir))540isp = dsp;541542*value = kstrdup(isp, GFP_KERNEL);543if (*value == NULL)544return -ENOMEM;545}546547if (len)548*len = strlen(isp) + 1;549550return 0;551}552553/**554* smack_inode_link - Smack check on link555* @old_dentry: the existing object556* @dir: unused557* @new_dentry: the new object558*559* Returns 0 if access is permitted, an error code otherwise560*/561static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,562struct dentry *new_dentry)563{564char *isp;565struct smk_audit_info ad;566int rc;567568smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);569smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);570571isp = smk_of_inode(old_dentry->d_inode);572rc = smk_curacc(isp, MAY_WRITE, &ad);573574if (rc == 0 && new_dentry->d_inode != NULL) {575isp = smk_of_inode(new_dentry->d_inode);576smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);577rc = smk_curacc(isp, MAY_WRITE, &ad);578}579580return rc;581}582583/**584* smack_inode_unlink - Smack check on inode deletion585* @dir: containing directory object586* @dentry: file to unlink587*588* Returns 0 if current can write the containing directory589* and the object, error code otherwise590*/591static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)592{593struct inode *ip = dentry->d_inode;594struct smk_audit_info ad;595int rc;596597smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);598smk_ad_setfield_u_fs_path_dentry(&ad, dentry);599600/*601* You need write access to the thing you're unlinking602*/603rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);604if (rc == 0) {605/*606* You also need write access to the containing directory607*/608smk_ad_setfield_u_fs_path_dentry(&ad, NULL);609smk_ad_setfield_u_fs_inode(&ad, dir);610rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);611}612return rc;613}614615/**616* smack_inode_rmdir - Smack check on directory deletion617* @dir: containing directory object618* @dentry: directory to unlink619*620* Returns 0 if current can write the containing directory621* and the directory, error code otherwise622*/623static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)624{625struct smk_audit_info ad;626int rc;627628smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);629smk_ad_setfield_u_fs_path_dentry(&ad, dentry);630631/*632* You need write access to the thing you're removing633*/634rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);635if (rc == 0) {636/*637* You also need write access to the containing directory638*/639smk_ad_setfield_u_fs_path_dentry(&ad, NULL);640smk_ad_setfield_u_fs_inode(&ad, dir);641rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);642}643644return rc;645}646647/**648* smack_inode_rename - Smack check on rename649* @old_inode: the old directory650* @old_dentry: unused651* @new_inode: the new directory652* @new_dentry: unused653*654* Read and write access is required on both the old and655* new directories.656*657* Returns 0 if access is permitted, an error code otherwise658*/659static int smack_inode_rename(struct inode *old_inode,660struct dentry *old_dentry,661struct inode *new_inode,662struct dentry *new_dentry)663{664int rc;665char *isp;666struct smk_audit_info ad;667668smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);669smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);670671isp = smk_of_inode(old_dentry->d_inode);672rc = smk_curacc(isp, MAY_READWRITE, &ad);673674if (rc == 0 && new_dentry->d_inode != NULL) {675isp = smk_of_inode(new_dentry->d_inode);676smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);677rc = smk_curacc(isp, MAY_READWRITE, &ad);678}679return rc;680}681682/**683* smack_inode_permission - Smack version of permission()684* @inode: the inode in question685* @mask: the access requested686*687* This is the important Smack hook.688*689* Returns 0 if access is permitted, -EACCES otherwise690*/691static int smack_inode_permission(struct inode *inode, int mask, unsigned flags)692{693struct smk_audit_info ad;694695mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);696/*697* No permission to check. Existence test. Yup, it's there.698*/699if (mask == 0)700return 0;701702/* May be droppable after audit */703if (flags & IPERM_FLAG_RCU)704return -ECHILD;705smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);706smk_ad_setfield_u_fs_inode(&ad, inode);707return smk_curacc(smk_of_inode(inode), mask, &ad);708}709710/**711* smack_inode_setattr - Smack check for setting attributes712* @dentry: the object713* @iattr: for the force flag714*715* Returns 0 if access is permitted, an error code otherwise716*/717static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)718{719struct smk_audit_info ad;720/*721* Need to allow for clearing the setuid bit.722*/723if (iattr->ia_valid & ATTR_FORCE)724return 0;725smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);726smk_ad_setfield_u_fs_path_dentry(&ad, dentry);727728return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);729}730731/**732* smack_inode_getattr - Smack check for getting attributes733* @mnt: unused734* @dentry: the object735*736* Returns 0 if access is permitted, an error code otherwise737*/738static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)739{740struct smk_audit_info ad;741struct path path;742743path.dentry = dentry;744path.mnt = mnt;745746smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);747smk_ad_setfield_u_fs_path(&ad, path);748return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);749}750751/**752* smack_inode_setxattr - Smack check for setting xattrs753* @dentry: the object754* @name: name of the attribute755* @value: unused756* @size: unused757* @flags: unused758*759* This protects the Smack attribute explicitly.760*761* Returns 0 if access is permitted, an error code otherwise762*/763static int smack_inode_setxattr(struct dentry *dentry, const char *name,764const void *value, size_t size, int flags)765{766struct smk_audit_info ad;767int rc = 0;768769if (strcmp(name, XATTR_NAME_SMACK) == 0 ||770strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||771strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||772strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||773strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {774if (!capable(CAP_MAC_ADMIN))775rc = -EPERM;776/*777* check label validity here so import wont fail on778* post_setxattr779*/780if (size == 0 || size >= SMK_LABELLEN ||781smk_import(value, size) == NULL)782rc = -EINVAL;783} else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {784if (!capable(CAP_MAC_ADMIN))785rc = -EPERM;786if (size != TRANS_TRUE_SIZE ||787strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)788rc = -EINVAL;789} else790rc = cap_inode_setxattr(dentry, name, value, size, flags);791792smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);793smk_ad_setfield_u_fs_path_dentry(&ad, dentry);794795if (rc == 0)796rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);797798return rc;799}800801/**802* smack_inode_post_setxattr - Apply the Smack update approved above803* @dentry: object804* @name: attribute name805* @value: attribute value806* @size: attribute size807* @flags: unused808*809* Set the pointer in the inode blob to the entry found810* in the master label list.811*/812static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,813const void *value, size_t size, int flags)814{815char *nsp;816struct inode_smack *isp = dentry->d_inode->i_security;817818if (strcmp(name, XATTR_NAME_SMACK) == 0) {819nsp = smk_import(value, size);820if (nsp != NULL)821isp->smk_inode = nsp;822else823isp->smk_inode = smack_known_invalid.smk_known;824} else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {825nsp = smk_import(value, size);826if (nsp != NULL)827isp->smk_task = nsp;828else829isp->smk_task = smack_known_invalid.smk_known;830} else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {831nsp = smk_import(value, size);832if (nsp != NULL)833isp->smk_mmap = nsp;834else835isp->smk_mmap = smack_known_invalid.smk_known;836} else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)837isp->smk_flags |= SMK_INODE_TRANSMUTE;838839return;840}841842/*843* smack_inode_getxattr - Smack check on getxattr844* @dentry: the object845* @name: unused846*847* Returns 0 if access is permitted, an error code otherwise848*/849static int smack_inode_getxattr(struct dentry *dentry, const char *name)850{851struct smk_audit_info ad;852853smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);854smk_ad_setfield_u_fs_path_dentry(&ad, dentry);855856return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);857}858859/*860* smack_inode_removexattr - Smack check on removexattr861* @dentry: the object862* @name: name of the attribute863*864* Removing the Smack attribute requires CAP_MAC_ADMIN865*866* Returns 0 if access is permitted, an error code otherwise867*/868static int smack_inode_removexattr(struct dentry *dentry, const char *name)869{870struct inode_smack *isp;871struct smk_audit_info ad;872int rc = 0;873874if (strcmp(name, XATTR_NAME_SMACK) == 0 ||875strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||876strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||877strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||878strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||879strcmp(name, XATTR_NAME_SMACKMMAP)) {880if (!capable(CAP_MAC_ADMIN))881rc = -EPERM;882} else883rc = cap_inode_removexattr(dentry, name);884885smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);886smk_ad_setfield_u_fs_path_dentry(&ad, dentry);887if (rc == 0)888rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);889890if (rc == 0) {891isp = dentry->d_inode->i_security;892isp->smk_task = NULL;893isp->smk_mmap = NULL;894}895896return rc;897}898899/**900* smack_inode_getsecurity - get smack xattrs901* @inode: the object902* @name: attribute name903* @buffer: where to put the result904* @alloc: unused905*906* Returns the size of the attribute or an error code907*/908static int smack_inode_getsecurity(const struct inode *inode,909const char *name, void **buffer,910bool alloc)911{912struct socket_smack *ssp;913struct socket *sock;914struct super_block *sbp;915struct inode *ip = (struct inode *)inode;916char *isp;917int ilen;918int rc = 0;919920if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {921isp = smk_of_inode(inode);922ilen = strlen(isp) + 1;923*buffer = isp;924return ilen;925}926927/*928* The rest of the Smack xattrs are only on sockets.929*/930sbp = ip->i_sb;931if (sbp->s_magic != SOCKFS_MAGIC)932return -EOPNOTSUPP;933934sock = SOCKET_I(ip);935if (sock == NULL || sock->sk == NULL)936return -EOPNOTSUPP;937938ssp = sock->sk->sk_security;939940if (strcmp(name, XATTR_SMACK_IPIN) == 0)941isp = ssp->smk_in;942else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)943isp = ssp->smk_out;944else945return -EOPNOTSUPP;946947ilen = strlen(isp) + 1;948if (rc == 0) {949*buffer = isp;950rc = ilen;951}952953return rc;954}955956957/**958* smack_inode_listsecurity - list the Smack attributes959* @inode: the object960* @buffer: where they go961* @buffer_size: size of buffer962*963* Returns 0 on success, -EINVAL otherwise964*/965static int smack_inode_listsecurity(struct inode *inode, char *buffer,966size_t buffer_size)967{968int len = strlen(XATTR_NAME_SMACK);969970if (buffer != NULL && len <= buffer_size) {971memcpy(buffer, XATTR_NAME_SMACK, len);972return len;973}974return -EINVAL;975}976977/**978* smack_inode_getsecid - Extract inode's security id979* @inode: inode to extract the info from980* @secid: where result will be saved981*/982static void smack_inode_getsecid(const struct inode *inode, u32 *secid)983{984struct inode_smack *isp = inode->i_security;985986*secid = smack_to_secid(isp->smk_inode);987}988989/*990* File Hooks991*/992993/**994* smack_file_permission - Smack check on file operations995* @file: unused996* @mask: unused997*998* Returns 0999*1000* Should access checks be done on each read or write?1001* UNICOS and SELinux say yes.1002* Trusted Solaris, Trusted Irix, and just about everyone else says no.1003*1004* I'll say no for now. Smack does not do the frequent1005* label changing that SELinux does.1006*/1007static int smack_file_permission(struct file *file, int mask)1008{1009return 0;1010}10111012/**1013* smack_file_alloc_security - assign a file security blob1014* @file: the object1015*1016* The security blob for a file is a pointer to the master1017* label list, so no allocation is done.1018*1019* Returns 01020*/1021static int smack_file_alloc_security(struct file *file)1022{1023file->f_security = smk_of_current();1024return 0;1025}10261027/**1028* smack_file_free_security - clear a file security blob1029* @file: the object1030*1031* The security blob for a file is a pointer to the master1032* label list, so no memory is freed.1033*/1034static void smack_file_free_security(struct file *file)1035{1036file->f_security = NULL;1037}10381039/**1040* smack_file_ioctl - Smack check on ioctls1041* @file: the object1042* @cmd: what to do1043* @arg: unused1044*1045* Relies heavily on the correct use of the ioctl command conventions.1046*1047* Returns 0 if allowed, error code otherwise1048*/1049static int smack_file_ioctl(struct file *file, unsigned int cmd,1050unsigned long arg)1051{1052int rc = 0;1053struct smk_audit_info ad;10541055smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);1056smk_ad_setfield_u_fs_path(&ad, file->f_path);10571058if (_IOC_DIR(cmd) & _IOC_WRITE)1059rc = smk_curacc(file->f_security, MAY_WRITE, &ad);10601061if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))1062rc = smk_curacc(file->f_security, MAY_READ, &ad);10631064return rc;1065}10661067/**1068* smack_file_lock - Smack check on file locking1069* @file: the object1070* @cmd: unused1071*1072* Returns 0 if current has write access, error code otherwise1073*/1074static int smack_file_lock(struct file *file, unsigned int cmd)1075{1076struct smk_audit_info ad;10771078smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);1079smk_ad_setfield_u_fs_path(&ad, file->f_path);1080return smk_curacc(file->f_security, MAY_WRITE, &ad);1081}10821083/**1084* smack_file_fcntl - Smack check on fcntl1085* @file: the object1086* @cmd: what action to check1087* @arg: unused1088*1089* Returns 0 if current has access, error code otherwise1090*/1091static int smack_file_fcntl(struct file *file, unsigned int cmd,1092unsigned long arg)1093{1094struct smk_audit_info ad;1095int rc;10961097smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);1098smk_ad_setfield_u_fs_path(&ad, file->f_path);10991100switch (cmd) {1101case F_DUPFD:1102case F_GETFD:1103case F_GETFL:1104case F_GETLK:1105case F_GETOWN:1106case F_GETSIG:1107rc = smk_curacc(file->f_security, MAY_READ, &ad);1108break;1109case F_SETFD:1110case F_SETFL:1111case F_SETLK:1112case F_SETLKW:1113case F_SETOWN:1114case F_SETSIG:1115rc = smk_curacc(file->f_security, MAY_WRITE, &ad);1116break;1117default:1118rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);1119}11201121return rc;1122}11231124/**1125* smack_file_mmap :1126* Check permissions for a mmap operation. The @file may be NULL, e.g.1127* if mapping anonymous memory.1128* @file contains the file structure for file to map (may be NULL).1129* @reqprot contains the protection requested by the application.1130* @prot contains the protection that will be applied by the kernel.1131* @flags contains the operational flags.1132* Return 0 if permission is granted.1133*/1134static int smack_file_mmap(struct file *file,1135unsigned long reqprot, unsigned long prot,1136unsigned long flags, unsigned long addr,1137unsigned long addr_only)1138{1139struct smack_rule *srp;1140struct task_smack *tsp;1141char *sp;1142char *msmack;1143char *osmack;1144struct inode_smack *isp;1145struct dentry *dp;1146int may;1147int mmay;1148int tmay;1149int rc;11501151/* do DAC check on address space usage */1152rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);1153if (rc || addr_only)1154return rc;11551156if (file == NULL || file->f_dentry == NULL)1157return 0;11581159dp = file->f_dentry;11601161if (dp->d_inode == NULL)1162return 0;11631164isp = dp->d_inode->i_security;1165if (isp->smk_mmap == NULL)1166return 0;1167msmack = isp->smk_mmap;11681169tsp = current_security();1170sp = smk_of_current();1171rc = 0;11721173rcu_read_lock();1174/*1175* For each Smack rule associated with the subject1176* label verify that the SMACK64MMAP also has access1177* to that rule's object label.1178*1179* Because neither of the labels comes1180* from the networking code it is sufficient1181* to compare pointers.1182*/1183list_for_each_entry_rcu(srp, &smack_rule_list, list) {1184if (srp->smk_subject != sp)1185continue;11861187osmack = srp->smk_object;1188/*1189* Matching labels always allows access.1190*/1191if (msmack == osmack)1192continue;1193/*1194* If there is a matching local rule take1195* that into account as well.1196*/1197may = smk_access_entry(srp->smk_subject, osmack,1198&tsp->smk_rules);1199if (may == -ENOENT)1200may = srp->smk_access;1201else1202may &= srp->smk_access;1203/*1204* If may is zero the SMACK64MMAP subject can't1205* possibly have less access.1206*/1207if (may == 0)1208continue;12091210/*1211* Fetch the global list entry.1212* If there isn't one a SMACK64MMAP subject1213* can't have as much access as current.1214*/1215mmay = smk_access_entry(msmack, osmack, &smack_rule_list);1216if (mmay == -ENOENT) {1217rc = -EACCES;1218break;1219}1220/*1221* If there is a local entry it modifies the1222* potential access, too.1223*/1224tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);1225if (tmay != -ENOENT)1226mmay &= tmay;12271228/*1229* If there is any access available to current that is1230* not available to a SMACK64MMAP subject1231* deny access.1232*/1233if ((may | mmay) != mmay) {1234rc = -EACCES;1235break;1236}1237}12381239rcu_read_unlock();12401241return rc;1242}12431244/**1245* smack_file_set_fowner - set the file security blob value1246* @file: object in question1247*1248* Returns 01249* Further research may be required on this one.1250*/1251static int smack_file_set_fowner(struct file *file)1252{1253file->f_security = smk_of_current();1254return 0;1255}12561257/**1258* smack_file_send_sigiotask - Smack on sigio1259* @tsk: The target task1260* @fown: the object the signal come from1261* @signum: unused1262*1263* Allow a privileged task to get signals even if it shouldn't1264*1265* Returns 0 if a subject with the object's smack could1266* write to the task, an error code otherwise.1267*/1268static int smack_file_send_sigiotask(struct task_struct *tsk,1269struct fown_struct *fown, int signum)1270{1271struct file *file;1272int rc;1273char *tsp = smk_of_task(tsk->cred->security);1274struct smk_audit_info ad;12751276/*1277* struct fown_struct is never outside the context of a struct file1278*/1279file = container_of(fown, struct file, f_owner);12801281/* we don't log here as rc can be overriden */1282rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);1283if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))1284rc = 0;12851286smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);1287smk_ad_setfield_u_tsk(&ad, tsk);1288smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);1289return rc;1290}12911292/**1293* smack_file_receive - Smack file receive check1294* @file: the object1295*1296* Returns 0 if current has access, error code otherwise1297*/1298static int smack_file_receive(struct file *file)1299{1300int may = 0;1301struct smk_audit_info ad;13021303smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);1304smk_ad_setfield_u_fs_path(&ad, file->f_path);1305/*1306* This code relies on bitmasks.1307*/1308if (file->f_mode & FMODE_READ)1309may = MAY_READ;1310if (file->f_mode & FMODE_WRITE)1311may |= MAY_WRITE;13121313return smk_curacc(file->f_security, may, &ad);1314}13151316/*1317* Task hooks1318*/13191320/**1321* smack_cred_alloc_blank - "allocate" blank task-level security credentials1322* @new: the new credentials1323* @gfp: the atomicity of any memory allocations1324*1325* Prepare a blank set of credentials for modification. This must allocate all1326* the memory the LSM module might require such that cred_transfer() can1327* complete without error.1328*/1329static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)1330{1331struct task_smack *tsp;13321333tsp = new_task_smack(NULL, NULL, gfp);1334if (tsp == NULL)1335return -ENOMEM;13361337cred->security = tsp;13381339return 0;1340}134113421343/**1344* smack_cred_free - "free" task-level security credentials1345* @cred: the credentials in question1346*1347*/1348static void smack_cred_free(struct cred *cred)1349{1350struct task_smack *tsp = cred->security;1351struct smack_rule *rp;1352struct list_head *l;1353struct list_head *n;13541355if (tsp == NULL)1356return;1357cred->security = NULL;13581359list_for_each_safe(l, n, &tsp->smk_rules) {1360rp = list_entry(l, struct smack_rule, list);1361list_del(&rp->list);1362kfree(rp);1363}1364kfree(tsp);1365}13661367/**1368* smack_cred_prepare - prepare new set of credentials for modification1369* @new: the new credentials1370* @old: the original credentials1371* @gfp: the atomicity of any memory allocations1372*1373* Prepare a new set of credentials for modification.1374*/1375static int smack_cred_prepare(struct cred *new, const struct cred *old,1376gfp_t gfp)1377{1378struct task_smack *old_tsp = old->security;1379struct task_smack *new_tsp;1380int rc;13811382new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);1383if (new_tsp == NULL)1384return -ENOMEM;13851386rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);1387if (rc != 0)1388return rc;13891390new->security = new_tsp;1391return 0;1392}13931394/**1395* smack_cred_transfer - Transfer the old credentials to the new credentials1396* @new: the new credentials1397* @old: the original credentials1398*1399* Fill in a set of blank credentials from another set of credentials.1400*/1401static void smack_cred_transfer(struct cred *new, const struct cred *old)1402{1403struct task_smack *old_tsp = old->security;1404struct task_smack *new_tsp = new->security;14051406new_tsp->smk_task = old_tsp->smk_task;1407new_tsp->smk_forked = old_tsp->smk_task;1408mutex_init(&new_tsp->smk_rules_lock);1409INIT_LIST_HEAD(&new_tsp->smk_rules);141014111412/* cbs copy rule list */1413}14141415/**1416* smack_kernel_act_as - Set the subjective context in a set of credentials1417* @new: points to the set of credentials to be modified.1418* @secid: specifies the security ID to be set1419*1420* Set the security data for a kernel service.1421*/1422static int smack_kernel_act_as(struct cred *new, u32 secid)1423{1424struct task_smack *new_tsp = new->security;1425char *smack = smack_from_secid(secid);14261427if (smack == NULL)1428return -EINVAL;14291430new_tsp->smk_task = smack;1431return 0;1432}14331434/**1435* smack_kernel_create_files_as - Set the file creation label in a set of creds1436* @new: points to the set of credentials to be modified1437* @inode: points to the inode to use as a reference1438*1439* Set the file creation context in a set of credentials to the same1440* as the objective context of the specified inode1441*/1442static int smack_kernel_create_files_as(struct cred *new,1443struct inode *inode)1444{1445struct inode_smack *isp = inode->i_security;1446struct task_smack *tsp = new->security;14471448tsp->smk_forked = isp->smk_inode;1449tsp->smk_task = isp->smk_inode;1450return 0;1451}14521453/**1454* smk_curacc_on_task - helper to log task related access1455* @p: the task object1456* @access : the access requested1457*1458* Return 0 if access is permitted1459*/1460static int smk_curacc_on_task(struct task_struct *p, int access)1461{1462struct smk_audit_info ad;14631464smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);1465smk_ad_setfield_u_tsk(&ad, p);1466return smk_curacc(smk_of_task(task_security(p)), access, &ad);1467}14681469/**1470* smack_task_setpgid - Smack check on setting pgid1471* @p: the task object1472* @pgid: unused1473*1474* Return 0 if write access is permitted1475*/1476static int smack_task_setpgid(struct task_struct *p, pid_t pgid)1477{1478return smk_curacc_on_task(p, MAY_WRITE);1479}14801481/**1482* smack_task_getpgid - Smack access check for getpgid1483* @p: the object task1484*1485* Returns 0 if current can read the object task, error code otherwise1486*/1487static int smack_task_getpgid(struct task_struct *p)1488{1489return smk_curacc_on_task(p, MAY_READ);1490}14911492/**1493* smack_task_getsid - Smack access check for getsid1494* @p: the object task1495*1496* Returns 0 if current can read the object task, error code otherwise1497*/1498static int smack_task_getsid(struct task_struct *p)1499{1500return smk_curacc_on_task(p, MAY_READ);1501}15021503/**1504* smack_task_getsecid - get the secid of the task1505* @p: the object task1506* @secid: where to put the result1507*1508* Sets the secid to contain a u32 version of the smack label.1509*/1510static void smack_task_getsecid(struct task_struct *p, u32 *secid)1511{1512*secid = smack_to_secid(smk_of_task(task_security(p)));1513}15141515/**1516* smack_task_setnice - Smack check on setting nice1517* @p: the task object1518* @nice: unused1519*1520* Return 0 if write access is permitted1521*/1522static int smack_task_setnice(struct task_struct *p, int nice)1523{1524int rc;15251526rc = cap_task_setnice(p, nice);1527if (rc == 0)1528rc = smk_curacc_on_task(p, MAY_WRITE);1529return rc;1530}15311532/**1533* smack_task_setioprio - Smack check on setting ioprio1534* @p: the task object1535* @ioprio: unused1536*1537* Return 0 if write access is permitted1538*/1539static int smack_task_setioprio(struct task_struct *p, int ioprio)1540{1541int rc;15421543rc = cap_task_setioprio(p, ioprio);1544if (rc == 0)1545rc = smk_curacc_on_task(p, MAY_WRITE);1546return rc;1547}15481549/**1550* smack_task_getioprio - Smack check on reading ioprio1551* @p: the task object1552*1553* Return 0 if read access is permitted1554*/1555static int smack_task_getioprio(struct task_struct *p)1556{1557return smk_curacc_on_task(p, MAY_READ);1558}15591560/**1561* smack_task_setscheduler - Smack check on setting scheduler1562* @p: the task object1563* @policy: unused1564* @lp: unused1565*1566* Return 0 if read access is permitted1567*/1568static int smack_task_setscheduler(struct task_struct *p)1569{1570int rc;15711572rc = cap_task_setscheduler(p);1573if (rc == 0)1574rc = smk_curacc_on_task(p, MAY_WRITE);1575return rc;1576}15771578/**1579* smack_task_getscheduler - Smack check on reading scheduler1580* @p: the task object1581*1582* Return 0 if read access is permitted1583*/1584static int smack_task_getscheduler(struct task_struct *p)1585{1586return smk_curacc_on_task(p, MAY_READ);1587}15881589/**1590* smack_task_movememory - Smack check on moving memory1591* @p: the task object1592*1593* Return 0 if write access is permitted1594*/1595static int smack_task_movememory(struct task_struct *p)1596{1597return smk_curacc_on_task(p, MAY_WRITE);1598}15991600/**1601* smack_task_kill - Smack check on signal delivery1602* @p: the task object1603* @info: unused1604* @sig: unused1605* @secid: identifies the smack to use in lieu of current's1606*1607* Return 0 if write access is permitted1608*1609* The secid behavior is an artifact of an SELinux hack1610* in the USB code. Someday it may go away.1611*/1612static int smack_task_kill(struct task_struct *p, struct siginfo *info,1613int sig, u32 secid)1614{1615struct smk_audit_info ad;16161617smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);1618smk_ad_setfield_u_tsk(&ad, p);1619/*1620* Sending a signal requires that the sender1621* can write the receiver.1622*/1623if (secid == 0)1624return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,1625&ad);1626/*1627* If the secid isn't 0 we're dealing with some USB IO1628* specific behavior. This is not clean. For one thing1629* we can't take privilege into account.1630*/1631return smk_access(smack_from_secid(secid),1632smk_of_task(task_security(p)), MAY_WRITE, &ad);1633}16341635/**1636* smack_task_wait - Smack access check for waiting1637* @p: task to wait for1638*1639* Returns 0 if current can wait for p, error code otherwise1640*/1641static int smack_task_wait(struct task_struct *p)1642{1643struct smk_audit_info ad;1644char *sp = smk_of_current();1645char *tsp = smk_of_forked(task_security(p));1646int rc;16471648/* we don't log here, we can be overriden */1649rc = smk_access(tsp, sp, MAY_WRITE, NULL);1650if (rc == 0)1651goto out_log;16521653/*1654* Allow the operation to succeed if either task1655* has privilege to perform operations that might1656* account for the smack labels having gotten to1657* be different in the first place.1658*1659* This breaks the strict subject/object access1660* control ideal, taking the object's privilege1661* state into account in the decision as well as1662* the smack value.1663*/1664if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))1665rc = 0;1666/* we log only if we didn't get overriden */1667out_log:1668smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);1669smk_ad_setfield_u_tsk(&ad, p);1670smack_log(tsp, sp, MAY_WRITE, rc, &ad);1671return rc;1672}16731674/**1675* smack_task_to_inode - copy task smack into the inode blob1676* @p: task to copy from1677* @inode: inode to copy to1678*1679* Sets the smack pointer in the inode security blob1680*/1681static void smack_task_to_inode(struct task_struct *p, struct inode *inode)1682{1683struct inode_smack *isp = inode->i_security;1684isp->smk_inode = smk_of_task(task_security(p));1685}16861687/*1688* Socket hooks.1689*/16901691/**1692* smack_sk_alloc_security - Allocate a socket blob1693* @sk: the socket1694* @family: unused1695* @gfp_flags: memory allocation flags1696*1697* Assign Smack pointers to current1698*1699* Returns 0 on success, -ENOMEM is there's no memory1700*/1701static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)1702{1703char *csp = smk_of_current();1704struct socket_smack *ssp;17051706ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);1707if (ssp == NULL)1708return -ENOMEM;17091710ssp->smk_in = csp;1711ssp->smk_out = csp;1712ssp->smk_packet[0] = '\0';17131714sk->sk_security = ssp;17151716return 0;1717}17181719/**1720* smack_sk_free_security - Free a socket blob1721* @sk: the socket1722*1723* Clears the blob pointer1724*/1725static void smack_sk_free_security(struct sock *sk)1726{1727kfree(sk->sk_security);1728}17291730/**1731* smack_host_label - check host based restrictions1732* @sip: the object end1733*1734* looks for host based access restrictions1735*1736* This version will only be appropriate for really small sets of single label1737* hosts. The caller is responsible for ensuring that the RCU read lock is1738* taken before calling this function.1739*1740* Returns the label of the far end or NULL if it's not special.1741*/1742static char *smack_host_label(struct sockaddr_in *sip)1743{1744struct smk_netlbladdr *snp;1745struct in_addr *siap = &sip->sin_addr;17461747if (siap->s_addr == 0)1748return NULL;17491750list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)1751/*1752* we break after finding the first match because1753* the list is sorted from longest to shortest mask1754* so we have found the most specific match1755*/1756if ((&snp->smk_host.sin_addr)->s_addr ==1757(siap->s_addr & (&snp->smk_mask)->s_addr)) {1758/* we have found the special CIPSO option */1759if (snp->smk_label == smack_cipso_option)1760return NULL;1761return snp->smk_label;1762}17631764return NULL;1765}17661767/**1768* smack_set_catset - convert a capset to netlabel mls categories1769* @catset: the Smack categories1770* @sap: where to put the netlabel categories1771*1772* Allocates and fills attr.mls.cat1773*/1774static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)1775{1776unsigned char *cp;1777unsigned char m;1778int cat;1779int rc;1780int byte;17811782if (!catset)1783return;17841785sap->flags |= NETLBL_SECATTR_MLS_CAT;1786sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);1787sap->attr.mls.cat->startbit = 0;17881789for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)1790for (m = 0x80; m != 0; m >>= 1, cat++) {1791if ((m & *cp) == 0)1792continue;1793rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,1794cat, GFP_ATOMIC);1795}1796}17971798/**1799* smack_to_secattr - fill a secattr from a smack value1800* @smack: the smack value1801* @nlsp: where the result goes1802*1803* Casey says that CIPSO is good enough for now.1804* It can be used to effect.1805* It can also be abused to effect when necessary.1806* Apologies to the TSIG group in general and GW in particular.1807*/1808static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)1809{1810struct smack_cipso cipso;1811int rc;18121813nlsp->domain = smack;1814nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;18151816rc = smack_to_cipso(smack, &cipso);1817if (rc == 0) {1818nlsp->attr.mls.lvl = cipso.smk_level;1819smack_set_catset(cipso.smk_catset, nlsp);1820} else {1821nlsp->attr.mls.lvl = smack_cipso_direct;1822smack_set_catset(smack, nlsp);1823}1824}18251826/**1827* smack_netlabel - Set the secattr on a socket1828* @sk: the socket1829* @labeled: socket label scheme1830*1831* Convert the outbound smack value (smk_out) to a1832* secattr and attach it to the socket.1833*1834* Returns 0 on success or an error code1835*/1836static int smack_netlabel(struct sock *sk, int labeled)1837{1838struct socket_smack *ssp = sk->sk_security;1839struct netlbl_lsm_secattr secattr;1840int rc = 0;18411842/*1843* Usually the netlabel code will handle changing the1844* packet labeling based on the label.1845* The case of a single label host is different, because1846* a single label host should never get a labeled packet1847* even though the label is usually associated with a packet1848* label.1849*/1850local_bh_disable();1851bh_lock_sock_nested(sk);18521853if (ssp->smk_out == smack_net_ambient ||1854labeled == SMACK_UNLABELED_SOCKET)1855netlbl_sock_delattr(sk);1856else {1857netlbl_secattr_init(&secattr);1858smack_to_secattr(ssp->smk_out, &secattr);1859rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);1860netlbl_secattr_destroy(&secattr);1861}18621863bh_unlock_sock(sk);1864local_bh_enable();18651866return rc;1867}18681869/**1870* smack_netlbel_send - Set the secattr on a socket and perform access checks1871* @sk: the socket1872* @sap: the destination address1873*1874* Set the correct secattr for the given socket based on the destination1875* address and perform any outbound access checks needed.1876*1877* Returns 0 on success or an error code.1878*1879*/1880static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)1881{1882int rc;1883int sk_lbl;1884char *hostsp;1885struct socket_smack *ssp = sk->sk_security;1886struct smk_audit_info ad;18871888rcu_read_lock();1889hostsp = smack_host_label(sap);1890if (hostsp != NULL) {1891sk_lbl = SMACK_UNLABELED_SOCKET;1892#ifdef CONFIG_AUDIT1893smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);1894ad.a.u.net.family = sap->sin_family;1895ad.a.u.net.dport = sap->sin_port;1896ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;1897#endif1898rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);1899} else {1900sk_lbl = SMACK_CIPSO_SOCKET;1901rc = 0;1902}1903rcu_read_unlock();1904if (rc != 0)1905return rc;19061907return smack_netlabel(sk, sk_lbl);1908}19091910/**1911* smack_inode_setsecurity - set smack xattrs1912* @inode: the object1913* @name: attribute name1914* @value: attribute value1915* @size: size of the attribute1916* @flags: unused1917*1918* Sets the named attribute in the appropriate blob1919*1920* Returns 0 on success, or an error code1921*/1922static int smack_inode_setsecurity(struct inode *inode, const char *name,1923const void *value, size_t size, int flags)1924{1925char *sp;1926struct inode_smack *nsp = inode->i_security;1927struct socket_smack *ssp;1928struct socket *sock;1929int rc = 0;19301931if (value == NULL || size > SMK_LABELLEN || size == 0)1932return -EACCES;19331934sp = smk_import(value, size);1935if (sp == NULL)1936return -EINVAL;19371938if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {1939nsp->smk_inode = sp;1940nsp->smk_flags |= SMK_INODE_INSTANT;1941return 0;1942}1943/*1944* The rest of the Smack xattrs are only on sockets.1945*/1946if (inode->i_sb->s_magic != SOCKFS_MAGIC)1947return -EOPNOTSUPP;19481949sock = SOCKET_I(inode);1950if (sock == NULL || sock->sk == NULL)1951return -EOPNOTSUPP;19521953ssp = sock->sk->sk_security;19541955if (strcmp(name, XATTR_SMACK_IPIN) == 0)1956ssp->smk_in = sp;1957else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {1958ssp->smk_out = sp;1959if (sock->sk->sk_family != PF_UNIX) {1960rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);1961if (rc != 0)1962printk(KERN_WARNING1963"Smack: \"%s\" netlbl error %d.\n",1964__func__, -rc);1965}1966} else1967return -EOPNOTSUPP;19681969return 0;1970}19711972/**1973* smack_socket_post_create - finish socket setup1974* @sock: the socket1975* @family: protocol family1976* @type: unused1977* @protocol: unused1978* @kern: unused1979*1980* Sets the netlabel information on the socket1981*1982* Returns 0 on success, and error code otherwise1983*/1984static int smack_socket_post_create(struct socket *sock, int family,1985int type, int protocol, int kern)1986{1987if (family != PF_INET || sock->sk == NULL)1988return 0;1989/*1990* Set the outbound netlbl.1991*/1992return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);1993}19941995/**1996* smack_socket_connect - connect access check1997* @sock: the socket1998* @sap: the other end1999* @addrlen: size of sap2000*2001* Verifies that a connection may be possible2002*2003* Returns 0 on success, and error code otherwise2004*/2005static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,2006int addrlen)2007{2008if (sock->sk == NULL || sock->sk->sk_family != PF_INET)2009return 0;2010if (addrlen < sizeof(struct sockaddr_in))2011return -EINVAL;20122013return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);2014}20152016/**2017* smack_flags_to_may - convert S_ to MAY_ values2018* @flags: the S_ value2019*2020* Returns the equivalent MAY_ value2021*/2022static int smack_flags_to_may(int flags)2023{2024int may = 0;20252026if (flags & S_IRUGO)2027may |= MAY_READ;2028if (flags & S_IWUGO)2029may |= MAY_WRITE;2030if (flags & S_IXUGO)2031may |= MAY_EXEC;20322033return may;2034}20352036/**2037* smack_msg_msg_alloc_security - Set the security blob for msg_msg2038* @msg: the object2039*2040* Returns 02041*/2042static int smack_msg_msg_alloc_security(struct msg_msg *msg)2043{2044msg->security = smk_of_current();2045return 0;2046}20472048/**2049* smack_msg_msg_free_security - Clear the security blob for msg_msg2050* @msg: the object2051*2052* Clears the blob pointer2053*/2054static void smack_msg_msg_free_security(struct msg_msg *msg)2055{2056msg->security = NULL;2057}20582059/**2060* smack_of_shm - the smack pointer for the shm2061* @shp: the object2062*2063* Returns a pointer to the smack value2064*/2065static char *smack_of_shm(struct shmid_kernel *shp)2066{2067return (char *)shp->shm_perm.security;2068}20692070/**2071* smack_shm_alloc_security - Set the security blob for shm2072* @shp: the object2073*2074* Returns 02075*/2076static int smack_shm_alloc_security(struct shmid_kernel *shp)2077{2078struct kern_ipc_perm *isp = &shp->shm_perm;20792080isp->security = smk_of_current();2081return 0;2082}20832084/**2085* smack_shm_free_security - Clear the security blob for shm2086* @shp: the object2087*2088* Clears the blob pointer2089*/2090static void smack_shm_free_security(struct shmid_kernel *shp)2091{2092struct kern_ipc_perm *isp = &shp->shm_perm;20932094isp->security = NULL;2095}20962097/**2098* smk_curacc_shm : check if current has access on shm2099* @shp : the object2100* @access : access requested2101*2102* Returns 0 if current has the requested access, error code otherwise2103*/2104static int smk_curacc_shm(struct shmid_kernel *shp, int access)2105{2106char *ssp = smack_of_shm(shp);2107struct smk_audit_info ad;21082109#ifdef CONFIG_AUDIT2110smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);2111ad.a.u.ipc_id = shp->shm_perm.id;2112#endif2113return smk_curacc(ssp, access, &ad);2114}21152116/**2117* smack_shm_associate - Smack access check for shm2118* @shp: the object2119* @shmflg: access requested2120*2121* Returns 0 if current has the requested access, error code otherwise2122*/2123static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)2124{2125int may;21262127may = smack_flags_to_may(shmflg);2128return smk_curacc_shm(shp, may);2129}21302131/**2132* smack_shm_shmctl - Smack access check for shm2133* @shp: the object2134* @cmd: what it wants to do2135*2136* Returns 0 if current has the requested access, error code otherwise2137*/2138static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)2139{2140int may;21412142switch (cmd) {2143case IPC_STAT:2144case SHM_STAT:2145may = MAY_READ;2146break;2147case IPC_SET:2148case SHM_LOCK:2149case SHM_UNLOCK:2150case IPC_RMID:2151may = MAY_READWRITE;2152break;2153case IPC_INFO:2154case SHM_INFO:2155/*2156* System level information.2157*/2158return 0;2159default:2160return -EINVAL;2161}2162return smk_curacc_shm(shp, may);2163}21642165/**2166* smack_shm_shmat - Smack access for shmat2167* @shp: the object2168* @shmaddr: unused2169* @shmflg: access requested2170*2171* Returns 0 if current has the requested access, error code otherwise2172*/2173static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,2174int shmflg)2175{2176int may;21772178may = smack_flags_to_may(shmflg);2179return smk_curacc_shm(shp, may);2180}21812182/**2183* smack_of_sem - the smack pointer for the sem2184* @sma: the object2185*2186* Returns a pointer to the smack value2187*/2188static char *smack_of_sem(struct sem_array *sma)2189{2190return (char *)sma->sem_perm.security;2191}21922193/**2194* smack_sem_alloc_security - Set the security blob for sem2195* @sma: the object2196*2197* Returns 02198*/2199static int smack_sem_alloc_security(struct sem_array *sma)2200{2201struct kern_ipc_perm *isp = &sma->sem_perm;22022203isp->security = smk_of_current();2204return 0;2205}22062207/**2208* smack_sem_free_security - Clear the security blob for sem2209* @sma: the object2210*2211* Clears the blob pointer2212*/2213static void smack_sem_free_security(struct sem_array *sma)2214{2215struct kern_ipc_perm *isp = &sma->sem_perm;22162217isp->security = NULL;2218}22192220/**2221* smk_curacc_sem : check if current has access on sem2222* @sma : the object2223* @access : access requested2224*2225* Returns 0 if current has the requested access, error code otherwise2226*/2227static int smk_curacc_sem(struct sem_array *sma, int access)2228{2229char *ssp = smack_of_sem(sma);2230struct smk_audit_info ad;22312232#ifdef CONFIG_AUDIT2233smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);2234ad.a.u.ipc_id = sma->sem_perm.id;2235#endif2236return smk_curacc(ssp, access, &ad);2237}22382239/**2240* smack_sem_associate - Smack access check for sem2241* @sma: the object2242* @semflg: access requested2243*2244* Returns 0 if current has the requested access, error code otherwise2245*/2246static int smack_sem_associate(struct sem_array *sma, int semflg)2247{2248int may;22492250may = smack_flags_to_may(semflg);2251return smk_curacc_sem(sma, may);2252}22532254/**2255* smack_sem_shmctl - Smack access check for sem2256* @sma: the object2257* @cmd: what it wants to do2258*2259* Returns 0 if current has the requested access, error code otherwise2260*/2261static int smack_sem_semctl(struct sem_array *sma, int cmd)2262{2263int may;22642265switch (cmd) {2266case GETPID:2267case GETNCNT:2268case GETZCNT:2269case GETVAL:2270case GETALL:2271case IPC_STAT:2272case SEM_STAT:2273may = MAY_READ;2274break;2275case SETVAL:2276case SETALL:2277case IPC_RMID:2278case IPC_SET:2279may = MAY_READWRITE;2280break;2281case IPC_INFO:2282case SEM_INFO:2283/*2284* System level information2285*/2286return 0;2287default:2288return -EINVAL;2289}22902291return smk_curacc_sem(sma, may);2292}22932294/**2295* smack_sem_semop - Smack checks of semaphore operations2296* @sma: the object2297* @sops: unused2298* @nsops: unused2299* @alter: unused2300*2301* Treated as read and write in all cases.2302*2303* Returns 0 if access is allowed, error code otherwise2304*/2305static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,2306unsigned nsops, int alter)2307{2308return smk_curacc_sem(sma, MAY_READWRITE);2309}23102311/**2312* smack_msg_alloc_security - Set the security blob for msg2313* @msq: the object2314*2315* Returns 02316*/2317static int smack_msg_queue_alloc_security(struct msg_queue *msq)2318{2319struct kern_ipc_perm *kisp = &msq->q_perm;23202321kisp->security = smk_of_current();2322return 0;2323}23242325/**2326* smack_msg_free_security - Clear the security blob for msg2327* @msq: the object2328*2329* Clears the blob pointer2330*/2331static void smack_msg_queue_free_security(struct msg_queue *msq)2332{2333struct kern_ipc_perm *kisp = &msq->q_perm;23342335kisp->security = NULL;2336}23372338/**2339* smack_of_msq - the smack pointer for the msq2340* @msq: the object2341*2342* Returns a pointer to the smack value2343*/2344static char *smack_of_msq(struct msg_queue *msq)2345{2346return (char *)msq->q_perm.security;2347}23482349/**2350* smk_curacc_msq : helper to check if current has access on msq2351* @msq : the msq2352* @access : access requested2353*2354* return 0 if current has access, error otherwise2355*/2356static int smk_curacc_msq(struct msg_queue *msq, int access)2357{2358char *msp = smack_of_msq(msq);2359struct smk_audit_info ad;23602361#ifdef CONFIG_AUDIT2362smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);2363ad.a.u.ipc_id = msq->q_perm.id;2364#endif2365return smk_curacc(msp, access, &ad);2366}23672368/**2369* smack_msg_queue_associate - Smack access check for msg_queue2370* @msq: the object2371* @msqflg: access requested2372*2373* Returns 0 if current has the requested access, error code otherwise2374*/2375static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)2376{2377int may;23782379may = smack_flags_to_may(msqflg);2380return smk_curacc_msq(msq, may);2381}23822383/**2384* smack_msg_queue_msgctl - Smack access check for msg_queue2385* @msq: the object2386* @cmd: what it wants to do2387*2388* Returns 0 if current has the requested access, error code otherwise2389*/2390static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)2391{2392int may;23932394switch (cmd) {2395case IPC_STAT:2396case MSG_STAT:2397may = MAY_READ;2398break;2399case IPC_SET:2400case IPC_RMID:2401may = MAY_READWRITE;2402break;2403case IPC_INFO:2404case MSG_INFO:2405/*2406* System level information2407*/2408return 0;2409default:2410return -EINVAL;2411}24122413return smk_curacc_msq(msq, may);2414}24152416/**2417* smack_msg_queue_msgsnd - Smack access check for msg_queue2418* @msq: the object2419* @msg: unused2420* @msqflg: access requested2421*2422* Returns 0 if current has the requested access, error code otherwise2423*/2424static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,2425int msqflg)2426{2427int may;24282429may = smack_flags_to_may(msqflg);2430return smk_curacc_msq(msq, may);2431}24322433/**2434* smack_msg_queue_msgsnd - Smack access check for msg_queue2435* @msq: the object2436* @msg: unused2437* @target: unused2438* @type: unused2439* @mode: unused2440*2441* Returns 0 if current has read and write access, error code otherwise2442*/2443static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,2444struct task_struct *target, long type, int mode)2445{2446return smk_curacc_msq(msq, MAY_READWRITE);2447}24482449/**2450* smack_ipc_permission - Smack access for ipc_permission()2451* @ipp: the object permissions2452* @flag: access requested2453*2454* Returns 0 if current has read and write access, error code otherwise2455*/2456static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)2457{2458char *isp = ipp->security;2459int may = smack_flags_to_may(flag);2460struct smk_audit_info ad;24612462#ifdef CONFIG_AUDIT2463smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);2464ad.a.u.ipc_id = ipp->id;2465#endif2466return smk_curacc(isp, may, &ad);2467}24682469/**2470* smack_ipc_getsecid - Extract smack security id2471* @ipp: the object permissions2472* @secid: where result will be saved2473*/2474static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)2475{2476char *smack = ipp->security;24772478*secid = smack_to_secid(smack);2479}24802481/**2482* smack_d_instantiate - Make sure the blob is correct on an inode2483* @opt_dentry: dentry where inode will be attached2484* @inode: the object2485*2486* Set the inode's security blob if it hasn't been done already.2487*/2488static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)2489{2490struct super_block *sbp;2491struct superblock_smack *sbsp;2492struct inode_smack *isp;2493char *csp = smk_of_current();2494char *fetched;2495char *final;2496char trattr[TRANS_TRUE_SIZE];2497int transflag = 0;2498struct dentry *dp;24992500if (inode == NULL)2501return;25022503isp = inode->i_security;25042505mutex_lock(&isp->smk_lock);2506/*2507* If the inode is already instantiated2508* take the quick way out2509*/2510if (isp->smk_flags & SMK_INODE_INSTANT)2511goto unlockandout;25122513sbp = inode->i_sb;2514sbsp = sbp->s_security;2515/*2516* We're going to use the superblock default label2517* if there's no label on the file.2518*/2519final = sbsp->smk_default;25202521/*2522* If this is the root inode the superblock2523* may be in the process of initialization.2524* If that is the case use the root value out2525* of the superblock.2526*/2527if (opt_dentry->d_parent == opt_dentry) {2528isp->smk_inode = sbsp->smk_root;2529isp->smk_flags |= SMK_INODE_INSTANT;2530goto unlockandout;2531}25322533/*2534* This is pretty hackish.2535* Casey says that we shouldn't have to do2536* file system specific code, but it does help2537* with keeping it simple.2538*/2539switch (sbp->s_magic) {2540case SMACK_MAGIC:2541/*2542* Casey says that it's a little embarrassing2543* that the smack file system doesn't do2544* extended attributes.2545*/2546final = smack_known_star.smk_known;2547break;2548case PIPEFS_MAGIC:2549/*2550* Casey says pipes are easy (?)2551*/2552final = smack_known_star.smk_known;2553break;2554case DEVPTS_SUPER_MAGIC:2555/*2556* devpts seems content with the label of the task.2557* Programs that change smack have to treat the2558* pty with respect.2559*/2560final = csp;2561break;2562case SOCKFS_MAGIC:2563/*2564* Socket access is controlled by the socket2565* structures associated with the task involved.2566*/2567final = smack_known_star.smk_known;2568break;2569case PROC_SUPER_MAGIC:2570/*2571* Casey says procfs appears not to care.2572* The superblock default suffices.2573*/2574break;2575case TMPFS_MAGIC:2576/*2577* Device labels should come from the filesystem,2578* but watch out, because they're volitile,2579* getting recreated on every reboot.2580*/2581final = smack_known_star.smk_known;2582/*2583* No break.2584*2585* If a smack value has been set we want to use it,2586* but since tmpfs isn't giving us the opportunity2587* to set mount options simulate setting the2588* superblock default.2589*/2590default:2591/*2592* This isn't an understood special case.2593* Get the value from the xattr.2594*/25952596/*2597* UNIX domain sockets use lower level socket data.2598*/2599if (S_ISSOCK(inode->i_mode)) {2600final = smack_known_star.smk_known;2601break;2602}2603/*2604* No xattr support means, alas, no SMACK label.2605* Use the aforeapplied default.2606* It would be curious if the label of the task2607* does not match that assigned.2608*/2609if (inode->i_op->getxattr == NULL)2610break;2611/*2612* Get the dentry for xattr.2613*/2614dp = dget(opt_dentry);2615fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);2616if (fetched != NULL) {2617final = fetched;2618if (S_ISDIR(inode->i_mode)) {2619trattr[0] = '\0';2620inode->i_op->getxattr(dp,2621XATTR_NAME_SMACKTRANSMUTE,2622trattr, TRANS_TRUE_SIZE);2623if (strncmp(trattr, TRANS_TRUE,2624TRANS_TRUE_SIZE) == 0)2625transflag = SMK_INODE_TRANSMUTE;2626}2627}2628isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);2629isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);26302631dput(dp);2632break;2633}26342635if (final == NULL)2636isp->smk_inode = csp;2637else2638isp->smk_inode = final;26392640isp->smk_flags |= (SMK_INODE_INSTANT | transflag);26412642unlockandout:2643mutex_unlock(&isp->smk_lock);2644return;2645}26462647/**2648* smack_getprocattr - Smack process attribute access2649* @p: the object task2650* @name: the name of the attribute in /proc/.../attr2651* @value: where to put the result2652*2653* Places a copy of the task Smack into value2654*2655* Returns the length of the smack label or an error code2656*/2657static int smack_getprocattr(struct task_struct *p, char *name, char **value)2658{2659char *cp;2660int slen;26612662if (strcmp(name, "current") != 0)2663return -EINVAL;26642665cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);2666if (cp == NULL)2667return -ENOMEM;26682669slen = strlen(cp);2670*value = cp;2671return slen;2672}26732674/**2675* smack_setprocattr - Smack process attribute setting2676* @p: the object task2677* @name: the name of the attribute in /proc/.../attr2678* @value: the value to set2679* @size: the size of the value2680*2681* Sets the Smack value of the task. Only setting self2682* is permitted and only with privilege2683*2684* Returns the length of the smack label or an error code2685*/2686static int smack_setprocattr(struct task_struct *p, char *name,2687void *value, size_t size)2688{2689int rc;2690struct task_smack *tsp;2691struct task_smack *oldtsp;2692struct cred *new;2693char *newsmack;26942695/*2696* Changing another process' Smack value is too dangerous2697* and supports no sane use case.2698*/2699if (p != current)2700return -EPERM;27012702if (!capable(CAP_MAC_ADMIN))2703return -EPERM;27042705if (value == NULL || size == 0 || size >= SMK_LABELLEN)2706return -EINVAL;27072708if (strcmp(name, "current") != 0)2709return -EINVAL;27102711newsmack = smk_import(value, size);2712if (newsmack == NULL)2713return -EINVAL;27142715/*2716* No process is ever allowed the web ("@") label.2717*/2718if (newsmack == smack_known_web.smk_known)2719return -EPERM;27202721oldtsp = p->cred->security;2722new = prepare_creds();2723if (new == NULL)2724return -ENOMEM;27252726tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);2727if (tsp == NULL) {2728kfree(new);2729return -ENOMEM;2730}2731rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);2732if (rc != 0)2733return rc;27342735new->security = tsp;2736commit_creds(new);2737return size;2738}27392740/**2741* smack_unix_stream_connect - Smack access on UDS2742* @sock: one sock2743* @other: the other sock2744* @newsk: unused2745*2746* Return 0 if a subject with the smack of sock could access2747* an object with the smack of other, otherwise an error code2748*/2749static int smack_unix_stream_connect(struct sock *sock,2750struct sock *other, struct sock *newsk)2751{2752struct socket_smack *ssp = sock->sk_security;2753struct socket_smack *osp = other->sk_security;2754struct smk_audit_info ad;2755int rc = 0;27562757smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);2758smk_ad_setfield_u_net_sk(&ad, other);27592760if (!capable(CAP_MAC_OVERRIDE))2761rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);27622763return rc;2764}27652766/**2767* smack_unix_may_send - Smack access on UDS2768* @sock: one socket2769* @other: the other socket2770*2771* Return 0 if a subject with the smack of sock could access2772* an object with the smack of other, otherwise an error code2773*/2774static int smack_unix_may_send(struct socket *sock, struct socket *other)2775{2776struct socket_smack *ssp = sock->sk->sk_security;2777struct socket_smack *osp = other->sk->sk_security;2778struct smk_audit_info ad;2779int rc = 0;27802781smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);2782smk_ad_setfield_u_net_sk(&ad, other->sk);27832784if (!capable(CAP_MAC_OVERRIDE))2785rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);27862787return rc;2788}27892790/**2791* smack_socket_sendmsg - Smack check based on destination host2792* @sock: the socket2793* @msg: the message2794* @size: the size of the message2795*2796* Return 0 if the current subject can write to the destination2797* host. This is only a question if the destination is a single2798* label host.2799*/2800static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,2801int size)2802{2803struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;28042805/*2806* Perfectly reasonable for this to be NULL2807*/2808if (sip == NULL || sip->sin_family != AF_INET)2809return 0;28102811return smack_netlabel_send(sock->sk, sip);2812}281328142815/**2816* smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack2817* @sap: netlabel secattr2818* @sip: where to put the result2819*2820* Copies a smack label into sip2821*/2822static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)2823{2824char smack[SMK_LABELLEN];2825char *sp;2826int pcat;28272828if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {2829/*2830* Looks like a CIPSO packet.2831* If there are flags but no level netlabel isn't2832* behaving the way we expect it to.2833*2834* Get the categories, if any2835* Without guidance regarding the smack value2836* for the packet fall back on the network2837* ambient value.2838*/2839memset(smack, '\0', SMK_LABELLEN);2840if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)2841for (pcat = -1;;) {2842pcat = netlbl_secattr_catmap_walk(2843sap->attr.mls.cat, pcat + 1);2844if (pcat < 0)2845break;2846smack_catset_bit(pcat, smack);2847}2848/*2849* If it is CIPSO using smack direct mapping2850* we are already done. WeeHee.2851*/2852if (sap->attr.mls.lvl == smack_cipso_direct) {2853memcpy(sip, smack, SMK_MAXLEN);2854return;2855}2856/*2857* Look it up in the supplied table if it is not2858* a direct mapping.2859*/2860smack_from_cipso(sap->attr.mls.lvl, smack, sip);2861return;2862}2863if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {2864/*2865* Looks like a fallback, which gives us a secid.2866*/2867sp = smack_from_secid(sap->attr.secid);2868/*2869* This has got to be a bug because it is2870* impossible to specify a fallback without2871* specifying the label, which will ensure2872* it has a secid, and the only way to get a2873* secid is from a fallback.2874*/2875BUG_ON(sp == NULL);2876strncpy(sip, sp, SMK_MAXLEN);2877return;2878}2879/*2880* Without guidance regarding the smack value2881* for the packet fall back on the network2882* ambient value.2883*/2884strncpy(sip, smack_net_ambient, SMK_MAXLEN);2885return;2886}28872888/**2889* smack_socket_sock_rcv_skb - Smack packet delivery access check2890* @sk: socket2891* @skb: packet2892*2893* Returns 0 if the packet should be delivered, an error code otherwise2894*/2895static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)2896{2897struct netlbl_lsm_secattr secattr;2898struct socket_smack *ssp = sk->sk_security;2899char smack[SMK_LABELLEN];2900char *csp;2901int rc;2902struct smk_audit_info ad;2903if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)2904return 0;29052906/*2907* Translate what netlabel gave us.2908*/2909netlbl_secattr_init(&secattr);29102911rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);2912if (rc == 0) {2913smack_from_secattr(&secattr, smack);2914csp = smack;2915} else2916csp = smack_net_ambient;29172918netlbl_secattr_destroy(&secattr);29192920#ifdef CONFIG_AUDIT2921smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);2922ad.a.u.net.family = sk->sk_family;2923ad.a.u.net.netif = skb->skb_iif;2924ipv4_skb_to_auditdata(skb, &ad.a, NULL);2925#endif2926/*2927* Receiving a packet requires that the other end2928* be able to write here. Read access is not required.2929* This is the simplist possible security model2930* for networking.2931*/2932rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);2933if (rc != 0)2934netlbl_skbuff_err(skb, rc, 0);2935return rc;2936}29372938/**2939* smack_socket_getpeersec_stream - pull in packet label2940* @sock: the socket2941* @optval: user's destination2942* @optlen: size thereof2943* @len: max thereof2944*2945* returns zero on success, an error code otherwise2946*/2947static int smack_socket_getpeersec_stream(struct socket *sock,2948char __user *optval,2949int __user *optlen, unsigned len)2950{2951struct socket_smack *ssp;2952int slen;2953int rc = 0;29542955ssp = sock->sk->sk_security;2956slen = strlen(ssp->smk_packet) + 1;29572958if (slen > len)2959rc = -ERANGE;2960else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)2961rc = -EFAULT;29622963if (put_user(slen, optlen) != 0)2964rc = -EFAULT;29652966return rc;2967}296829692970/**2971* smack_socket_getpeersec_dgram - pull in packet label2972* @sock: the peer socket2973* @skb: packet data2974* @secid: pointer to where to put the secid of the packet2975*2976* Sets the netlabel socket state on sk from parent2977*/2978static int smack_socket_getpeersec_dgram(struct socket *sock,2979struct sk_buff *skb, u32 *secid)29802981{2982struct netlbl_lsm_secattr secattr;2983struct socket_smack *sp;2984char smack[SMK_LABELLEN];2985int family = PF_UNSPEC;2986u32 s = 0; /* 0 is the invalid secid */2987int rc;29882989if (skb != NULL) {2990if (skb->protocol == htons(ETH_P_IP))2991family = PF_INET;2992else if (skb->protocol == htons(ETH_P_IPV6))2993family = PF_INET6;2994}2995if (family == PF_UNSPEC && sock != NULL)2996family = sock->sk->sk_family;29972998if (family == PF_UNIX) {2999sp = sock->sk->sk_security;3000s = smack_to_secid(sp->smk_out);3001} else if (family == PF_INET || family == PF_INET6) {3002/*3003* Translate what netlabel gave us.3004*/3005netlbl_secattr_init(&secattr);3006rc = netlbl_skbuff_getattr(skb, family, &secattr);3007if (rc == 0) {3008smack_from_secattr(&secattr, smack);3009s = smack_to_secid(smack);3010}3011netlbl_secattr_destroy(&secattr);3012}3013*secid = s;3014if (s == 0)3015return -EINVAL;3016return 0;3017}30183019/**3020* smack_sock_graft - Initialize a newly created socket with an existing sock3021* @sk: child sock3022* @parent: parent socket3023*3024* Set the smk_{in,out} state of an existing sock based on the process that3025* is creating the new socket.3026*/3027static void smack_sock_graft(struct sock *sk, struct socket *parent)3028{3029struct socket_smack *ssp;30303031if (sk == NULL ||3032(sk->sk_family != PF_INET && sk->sk_family != PF_INET6))3033return;30343035ssp = sk->sk_security;3036ssp->smk_in = ssp->smk_out = smk_of_current();3037/* cssp->smk_packet is already set in smack_inet_csk_clone() */3038}30393040/**3041* smack_inet_conn_request - Smack access check on connect3042* @sk: socket involved3043* @skb: packet3044* @req: unused3045*3046* Returns 0 if a task with the packet label could write to3047* the socket, otherwise an error code3048*/3049static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,3050struct request_sock *req)3051{3052u16 family = sk->sk_family;3053struct socket_smack *ssp = sk->sk_security;3054struct netlbl_lsm_secattr secattr;3055struct sockaddr_in addr;3056struct iphdr *hdr;3057char smack[SMK_LABELLEN];3058int rc;3059struct smk_audit_info ad;30603061/* handle mapped IPv4 packets arriving via IPv6 sockets */3062if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))3063family = PF_INET;30643065netlbl_secattr_init(&secattr);3066rc = netlbl_skbuff_getattr(skb, family, &secattr);3067if (rc == 0)3068smack_from_secattr(&secattr, smack);3069else3070strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);3071netlbl_secattr_destroy(&secattr);30723073#ifdef CONFIG_AUDIT3074smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);3075ad.a.u.net.family = family;3076ad.a.u.net.netif = skb->skb_iif;3077ipv4_skb_to_auditdata(skb, &ad.a, NULL);3078#endif3079/*3080* Receiving a packet requires that the other end be able to write3081* here. Read access is not required.3082*/3083rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);3084if (rc != 0)3085return rc;30863087/*3088* Save the peer's label in the request_sock so we can later setup3089* smk_packet in the child socket so that SO_PEERCRED can report it.3090*/3091req->peer_secid = smack_to_secid(smack);30923093/*3094* We need to decide if we want to label the incoming connection here3095* if we do we only need to label the request_sock and the stack will3096* propagate the wire-label to the sock when it is created.3097*/3098hdr = ip_hdr(skb);3099addr.sin_addr.s_addr = hdr->saddr;3100rcu_read_lock();3101if (smack_host_label(&addr) == NULL) {3102rcu_read_unlock();3103netlbl_secattr_init(&secattr);3104smack_to_secattr(smack, &secattr);3105rc = netlbl_req_setattr(req, &secattr);3106netlbl_secattr_destroy(&secattr);3107} else {3108rcu_read_unlock();3109netlbl_req_delattr(req);3110}31113112return rc;3113}31143115/**3116* smack_inet_csk_clone - Copy the connection information to the new socket3117* @sk: the new socket3118* @req: the connection's request_sock3119*3120* Transfer the connection's peer label to the newly created socket.3121*/3122static void smack_inet_csk_clone(struct sock *sk,3123const struct request_sock *req)3124{3125struct socket_smack *ssp = sk->sk_security;3126char *smack;31273128if (req->peer_secid != 0) {3129smack = smack_from_secid(req->peer_secid);3130strncpy(ssp->smk_packet, smack, SMK_MAXLEN);3131} else3132ssp->smk_packet[0] = '\0';3133}31343135/*3136* Key management security hooks3137*3138* Casey has not tested key support very heavily.3139* The permission check is most likely too restrictive.3140* If you care about keys please have a look.3141*/3142#ifdef CONFIG_KEYS31433144/**3145* smack_key_alloc - Set the key security blob3146* @key: object3147* @cred: the credentials to use3148* @flags: unused3149*3150* No allocation required3151*3152* Returns 03153*/3154static int smack_key_alloc(struct key *key, const struct cred *cred,3155unsigned long flags)3156{3157key->security = smk_of_task(cred->security);3158return 0;3159}31603161/**3162* smack_key_free - Clear the key security blob3163* @key: the object3164*3165* Clear the blob pointer3166*/3167static void smack_key_free(struct key *key)3168{3169key->security = NULL;3170}31713172/*3173* smack_key_permission - Smack access on a key3174* @key_ref: gets to the object3175* @cred: the credentials to use3176* @perm: unused3177*3178* Return 0 if the task has read and write to the object,3179* an error code otherwise3180*/3181static int smack_key_permission(key_ref_t key_ref,3182const struct cred *cred, key_perm_t perm)3183{3184struct key *keyp;3185struct smk_audit_info ad;3186char *tsp = smk_of_task(cred->security);31873188keyp = key_ref_to_ptr(key_ref);3189if (keyp == NULL)3190return -EINVAL;3191/*3192* If the key hasn't been initialized give it access so that3193* it may do so.3194*/3195if (keyp->security == NULL)3196return 0;3197/*3198* This should not occur3199*/3200if (tsp == NULL)3201return -EACCES;3202#ifdef CONFIG_AUDIT3203smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);3204ad.a.u.key_struct.key = keyp->serial;3205ad.a.u.key_struct.key_desc = keyp->description;3206#endif3207return smk_access(tsp, keyp->security,3208MAY_READWRITE, &ad);3209}3210#endif /* CONFIG_KEYS */32113212/*3213* Smack Audit hooks3214*3215* Audit requires a unique representation of each Smack specific3216* rule. This unique representation is used to distinguish the3217* object to be audited from remaining kernel objects and also3218* works as a glue between the audit hooks.3219*3220* Since repository entries are added but never deleted, we'll use3221* the smack_known label address related to the given audit rule as3222* the needed unique representation. This also better fits the smack3223* model where nearly everything is a label.3224*/3225#ifdef CONFIG_AUDIT32263227/**3228* smack_audit_rule_init - Initialize a smack audit rule3229* @field: audit rule fields given from user-space (audit.h)3230* @op: required testing operator (=, !=, >, <, ...)3231* @rulestr: smack label to be audited3232* @vrule: pointer to save our own audit rule representation3233*3234* Prepare to audit cases where (@field @op @rulestr) is true.3235* The label to be audited is created if necessay.3236*/3237static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)3238{3239char **rule = (char **)vrule;3240*rule = NULL;32413242if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)3243return -EINVAL;32443245if (op != Audit_equal && op != Audit_not_equal)3246return -EINVAL;32473248*rule = smk_import(rulestr, 0);32493250return 0;3251}32523253/**3254* smack_audit_rule_known - Distinguish Smack audit rules3255* @krule: rule of interest, in Audit kernel representation format3256*3257* This is used to filter Smack rules from remaining Audit ones.3258* If it's proved that this rule belongs to us, the3259* audit_rule_match hook will be called to do the final judgement.3260*/3261static int smack_audit_rule_known(struct audit_krule *krule)3262{3263struct audit_field *f;3264int i;32653266for (i = 0; i < krule->field_count; i++) {3267f = &krule->fields[i];32683269if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)3270return 1;3271}32723273return 0;3274}32753276/**3277* smack_audit_rule_match - Audit given object ?3278* @secid: security id for identifying the object to test3279* @field: audit rule flags given from user-space3280* @op: required testing operator3281* @vrule: smack internal rule presentation3282* @actx: audit context associated with the check3283*3284* The core Audit hook. It's used to take the decision of3285* whether to audit or not to audit a given object.3286*/3287static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,3288struct audit_context *actx)3289{3290char *smack;3291char *rule = vrule;32923293if (!rule) {3294audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,3295"Smack: missing rule\n");3296return -ENOENT;3297}32983299if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)3300return 0;33013302smack = smack_from_secid(secid);33033304/*3305* No need to do string comparisons. If a match occurs,3306* both pointers will point to the same smack_known3307* label.3308*/3309if (op == Audit_equal)3310return (rule == smack);3311if (op == Audit_not_equal)3312return (rule != smack);33133314return 0;3315}33163317/**3318* smack_audit_rule_free - free smack rule representation3319* @vrule: rule to be freed.3320*3321* No memory was allocated.3322*/3323static void smack_audit_rule_free(void *vrule)3324{3325/* No-op */3326}33273328#endif /* CONFIG_AUDIT */33293330/**3331* smack_secid_to_secctx - return the smack label for a secid3332* @secid: incoming integer3333* @secdata: destination3334* @seclen: how long it is3335*3336* Exists for networking code.3337*/3338static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)3339{3340char *sp = smack_from_secid(secid);33413342if (secdata)3343*secdata = sp;3344*seclen = strlen(sp);3345return 0;3346}33473348/**3349* smack_secctx_to_secid - return the secid for a smack label3350* @secdata: smack label3351* @seclen: how long result is3352* @secid: outgoing integer3353*3354* Exists for audit and networking code.3355*/3356static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)3357{3358*secid = smack_to_secid(secdata);3359return 0;3360}33613362/**3363* smack_release_secctx - don't do anything.3364* @secdata: unused3365* @seclen: unused3366*3367* Exists to make sure nothing gets done, and properly3368*/3369static void smack_release_secctx(char *secdata, u32 seclen)3370{3371}33723373static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)3374{3375return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);3376}33773378static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)3379{3380return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);3381}33823383static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)3384{3385int len = 0;3386len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);33873388if (len < 0)3389return len;3390*ctxlen = len;3391return 0;3392}33933394struct security_operations smack_ops = {3395.name = "smack",33963397.ptrace_access_check = smack_ptrace_access_check,3398.ptrace_traceme = smack_ptrace_traceme,3399.syslog = smack_syslog,34003401.sb_alloc_security = smack_sb_alloc_security,3402.sb_free_security = smack_sb_free_security,3403.sb_copy_data = smack_sb_copy_data,3404.sb_kern_mount = smack_sb_kern_mount,3405.sb_statfs = smack_sb_statfs,3406.sb_mount = smack_sb_mount,3407.sb_umount = smack_sb_umount,34083409.bprm_set_creds = smack_bprm_set_creds,34103411.inode_alloc_security = smack_inode_alloc_security,3412.inode_free_security = smack_inode_free_security,3413.inode_init_security = smack_inode_init_security,3414.inode_link = smack_inode_link,3415.inode_unlink = smack_inode_unlink,3416.inode_rmdir = smack_inode_rmdir,3417.inode_rename = smack_inode_rename,3418.inode_permission = smack_inode_permission,3419.inode_setattr = smack_inode_setattr,3420.inode_getattr = smack_inode_getattr,3421.inode_setxattr = smack_inode_setxattr,3422.inode_post_setxattr = smack_inode_post_setxattr,3423.inode_getxattr = smack_inode_getxattr,3424.inode_removexattr = smack_inode_removexattr,3425.inode_getsecurity = smack_inode_getsecurity,3426.inode_setsecurity = smack_inode_setsecurity,3427.inode_listsecurity = smack_inode_listsecurity,3428.inode_getsecid = smack_inode_getsecid,34293430.file_permission = smack_file_permission,3431.file_alloc_security = smack_file_alloc_security,3432.file_free_security = smack_file_free_security,3433.file_ioctl = smack_file_ioctl,3434.file_lock = smack_file_lock,3435.file_fcntl = smack_file_fcntl,3436.file_mmap = smack_file_mmap,3437.file_set_fowner = smack_file_set_fowner,3438.file_send_sigiotask = smack_file_send_sigiotask,3439.file_receive = smack_file_receive,34403441.cred_alloc_blank = smack_cred_alloc_blank,3442.cred_free = smack_cred_free,3443.cred_prepare = smack_cred_prepare,3444.cred_transfer = smack_cred_transfer,3445.kernel_act_as = smack_kernel_act_as,3446.kernel_create_files_as = smack_kernel_create_files_as,3447.task_setpgid = smack_task_setpgid,3448.task_getpgid = smack_task_getpgid,3449.task_getsid = smack_task_getsid,3450.task_getsecid = smack_task_getsecid,3451.task_setnice = smack_task_setnice,3452.task_setioprio = smack_task_setioprio,3453.task_getioprio = smack_task_getioprio,3454.task_setscheduler = smack_task_setscheduler,3455.task_getscheduler = smack_task_getscheduler,3456.task_movememory = smack_task_movememory,3457.task_kill = smack_task_kill,3458.task_wait = smack_task_wait,3459.task_to_inode = smack_task_to_inode,34603461.ipc_permission = smack_ipc_permission,3462.ipc_getsecid = smack_ipc_getsecid,34633464.msg_msg_alloc_security = smack_msg_msg_alloc_security,3465.msg_msg_free_security = smack_msg_msg_free_security,34663467.msg_queue_alloc_security = smack_msg_queue_alloc_security,3468.msg_queue_free_security = smack_msg_queue_free_security,3469.msg_queue_associate = smack_msg_queue_associate,3470.msg_queue_msgctl = smack_msg_queue_msgctl,3471.msg_queue_msgsnd = smack_msg_queue_msgsnd,3472.msg_queue_msgrcv = smack_msg_queue_msgrcv,34733474.shm_alloc_security = smack_shm_alloc_security,3475.shm_free_security = smack_shm_free_security,3476.shm_associate = smack_shm_associate,3477.shm_shmctl = smack_shm_shmctl,3478.shm_shmat = smack_shm_shmat,34793480.sem_alloc_security = smack_sem_alloc_security,3481.sem_free_security = smack_sem_free_security,3482.sem_associate = smack_sem_associate,3483.sem_semctl = smack_sem_semctl,3484.sem_semop = smack_sem_semop,34853486.d_instantiate = smack_d_instantiate,34873488.getprocattr = smack_getprocattr,3489.setprocattr = smack_setprocattr,34903491.unix_stream_connect = smack_unix_stream_connect,3492.unix_may_send = smack_unix_may_send,34933494.socket_post_create = smack_socket_post_create,3495.socket_connect = smack_socket_connect,3496.socket_sendmsg = smack_socket_sendmsg,3497.socket_sock_rcv_skb = smack_socket_sock_rcv_skb,3498.socket_getpeersec_stream = smack_socket_getpeersec_stream,3499.socket_getpeersec_dgram = smack_socket_getpeersec_dgram,3500.sk_alloc_security = smack_sk_alloc_security,3501.sk_free_security = smack_sk_free_security,3502.sock_graft = smack_sock_graft,3503.inet_conn_request = smack_inet_conn_request,3504.inet_csk_clone = smack_inet_csk_clone,35053506/* key management security hooks */3507#ifdef CONFIG_KEYS3508.key_alloc = smack_key_alloc,3509.key_free = smack_key_free,3510.key_permission = smack_key_permission,3511#endif /* CONFIG_KEYS */35123513/* Audit hooks */3514#ifdef CONFIG_AUDIT3515.audit_rule_init = smack_audit_rule_init,3516.audit_rule_known = smack_audit_rule_known,3517.audit_rule_match = smack_audit_rule_match,3518.audit_rule_free = smack_audit_rule_free,3519#endif /* CONFIG_AUDIT */35203521.secid_to_secctx = smack_secid_to_secctx,3522.secctx_to_secid = smack_secctx_to_secid,3523.release_secctx = smack_release_secctx,3524.inode_notifysecctx = smack_inode_notifysecctx,3525.inode_setsecctx = smack_inode_setsecctx,3526.inode_getsecctx = smack_inode_getsecctx,3527};352835293530static __init void init_smack_know_list(void)3531{3532list_add(&smack_known_huh.list, &smack_known_list);3533list_add(&smack_known_hat.list, &smack_known_list);3534list_add(&smack_known_star.list, &smack_known_list);3535list_add(&smack_known_floor.list, &smack_known_list);3536list_add(&smack_known_invalid.list, &smack_known_list);3537list_add(&smack_known_web.list, &smack_known_list);3538}35393540/**3541* smack_init - initialize the smack system3542*3543* Returns 03544*/3545static __init int smack_init(void)3546{3547struct cred *cred;3548struct task_smack *tsp;35493550if (!security_module_enable(&smack_ops))3551return 0;35523553tsp = new_task_smack(smack_known_floor.smk_known,3554smack_known_floor.smk_known, GFP_KERNEL);3555if (tsp == NULL)3556return -ENOMEM;35573558printk(KERN_INFO "Smack: Initializing.\n");35593560/*3561* Set the security state for the initial task.3562*/3563cred = (struct cred *) current->cred;3564cred->security = tsp;35653566/* initialize the smack_know_list */3567init_smack_know_list();3568/*3569* Initialize locks3570*/3571spin_lock_init(&smack_known_huh.smk_cipsolock);3572spin_lock_init(&smack_known_hat.smk_cipsolock);3573spin_lock_init(&smack_known_star.smk_cipsolock);3574spin_lock_init(&smack_known_floor.smk_cipsolock);3575spin_lock_init(&smack_known_invalid.smk_cipsolock);35763577/*3578* Register with LSM3579*/3580if (register_security(&smack_ops))3581panic("smack: Unable to register with kernel.\n");35823583return 0;3584}35853586/*3587* Smack requires early initialization in order to label3588* all processes and objects when they are created.3589*/3590security_initcall(smack_init);359135923593