0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _SECURITY_SMACK_H
0010 #define _SECURITY_SMACK_H
0011
0012 #include <linux/capability.h>
0013 #include <linux/spinlock.h>
0014 #include <linux/lsm_hooks.h>
0015 #include <linux/in.h>
0016 #if IS_ENABLED(CONFIG_IPV6)
0017 #include <linux/in6.h>
0018 #endif
0019 #include <net/netlabel.h>
0020 #include <linux/list.h>
0021 #include <linux/rculist.h>
0022 #include <linux/lsm_audit.h>
0023 #include <linux/msg.h>
0024
0025
0026
0027
0028
0029 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
0030 #define SMACK_IPV6_PORT_LABELING 1
0031 #endif
0032
0033 #if IS_ENABLED(CONFIG_IPV6) && defined(CONFIG_SECURITY_SMACK_NETFILTER)
0034 #define SMACK_IPV6_SECMARK_LABELING 1
0035 #endif
0036
0037
0038
0039
0040 #define SMK_LABELLEN 24
0041 #define SMK_LONGLABEL 256
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 struct smack_known {
0067 struct list_head list;
0068 struct hlist_node smk_hashed;
0069 char *smk_known;
0070 u32 smk_secid;
0071 struct netlbl_lsm_secattr smk_netlabel;
0072 struct list_head smk_rules;
0073 struct mutex smk_rules_lock;
0074 };
0075
0076
0077
0078
0079
0080
0081
0082
0083 #define SMK_CIPSOLEN 24
0084
0085 struct superblock_smack {
0086 struct smack_known *smk_root;
0087 struct smack_known *smk_floor;
0088 struct smack_known *smk_hat;
0089 struct smack_known *smk_default;
0090 int smk_flags;
0091 };
0092
0093
0094
0095
0096 #define SMK_SB_INITIALIZED 0x01
0097 #define SMK_SB_UNTRUSTED 0x02
0098
0099 struct socket_smack {
0100 struct smack_known *smk_out;
0101 struct smack_known *smk_in;
0102 struct smack_known *smk_packet;
0103 int smk_state;
0104 };
0105 #define SMK_NETLBL_UNSET 0
0106 #define SMK_NETLBL_UNLABELED 1
0107 #define SMK_NETLBL_LABELED 2
0108 #define SMK_NETLBL_REQSKB 3
0109
0110
0111
0112
0113 struct inode_smack {
0114 struct smack_known *smk_inode;
0115 struct smack_known *smk_task;
0116 struct smack_known *smk_mmap;
0117 int smk_flags;
0118 };
0119
0120 struct task_smack {
0121 struct smack_known *smk_task;
0122 struct smack_known *smk_forked;
0123 struct list_head smk_rules;
0124 struct mutex smk_rules_lock;
0125 struct list_head smk_relabel;
0126 };
0127
0128 #define SMK_INODE_INSTANT 0x01
0129 #define SMK_INODE_TRANSMUTE 0x02
0130 #define SMK_INODE_CHANGED 0x04
0131 #define SMK_INODE_IMPURE 0x08
0132
0133
0134
0135
0136 struct smack_rule {
0137 struct list_head list;
0138 struct smack_known *smk_subject;
0139 struct smack_known *smk_object;
0140 int smk_access;
0141 };
0142
0143
0144
0145
0146 struct smk_net4addr {
0147 struct list_head list;
0148 struct in_addr smk_host;
0149 struct in_addr smk_mask;
0150 int smk_masks;
0151 struct smack_known *smk_label;
0152 };
0153
0154
0155
0156
0157 struct smk_net6addr {
0158 struct list_head list;
0159 struct in6_addr smk_host;
0160 struct in6_addr smk_mask;
0161 int smk_masks;
0162 struct smack_known *smk_label;
0163 };
0164
0165
0166
0167
0168 struct smk_port_label {
0169 struct list_head list;
0170 struct sock *smk_sock;
0171 unsigned short smk_port;
0172 struct smack_known *smk_in;
0173 struct smack_known *smk_out;
0174 short smk_sock_type;
0175 short smk_can_reuse;
0176 };
0177
0178 struct smack_known_list_elem {
0179 struct list_head list;
0180 struct smack_known *smk_label;
0181 };
0182
0183
0184 #define FSDEFAULT_MNT 0x01
0185 #define FSFLOOR_MNT 0x02
0186 #define FSHAT_MNT 0x04
0187 #define FSROOT_MNT 0x08
0188 #define FSTRANS_MNT 0x10
0189
0190 #define NUM_SMK_MNT_OPTS 5
0191
0192 enum {
0193 Opt_error = -1,
0194 Opt_fsdefault = 0,
0195 Opt_fsfloor = 1,
0196 Opt_fshat = 2,
0197 Opt_fsroot = 3,
0198 Opt_fstransmute = 4,
0199 };
0200
0201 #define SMACK_DELETE_OPTION "-DELETE"
0202 #define SMACK_CIPSO_OPTION "-CIPSO"
0203
0204
0205
0206
0207 #define SMACK_CIPSO_DOI_DEFAULT 3
0208 #define SMACK_CIPSO_DOI_INVALID -1
0209 #define SMACK_CIPSO_DIRECT_DEFAULT 250
0210 #define SMACK_CIPSO_MAPPED_DEFAULT 251
0211 #define SMACK_CIPSO_MAXLEVEL 255
0212
0213
0214
0215
0216
0217 #define SMACK_CIPSO_MAXCATNUM 184
0218
0219
0220
0221
0222 #define SMACK_PTRACE_DEFAULT 0
0223 #define SMACK_PTRACE_EXACT 1
0224 #define SMACK_PTRACE_DRACONIAN 2
0225 #define SMACK_PTRACE_MAX SMACK_PTRACE_DRACONIAN
0226
0227
0228
0229
0230
0231
0232 #define MAY_TRANSMUTE 0x00001000
0233 #define MAY_LOCK 0x00002000
0234 #define MAY_BRINGUP 0x00004000
0235
0236
0237
0238
0239
0240 #ifdef CONFIG_SECURITY_SMACK_APPEND_SIGNALS
0241 #define MAY_DELIVER MAY_APPEND
0242 #else
0243 #define MAY_DELIVER MAY_WRITE
0244 #endif
0245
0246 #define SMACK_BRINGUP_ALLOW 1
0247 #define SMACK_UNCONFINED_SUBJECT 2
0248 #define SMACK_UNCONFINED_OBJECT 3
0249
0250
0251
0252
0253 #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
0254 #define MAY_READWRITE (MAY_READ | MAY_WRITE)
0255 #define MAY_NOT 0
0256
0257
0258
0259
0260 #define SMK_NUM_ACCESS_TYPE 7
0261
0262
0263 struct smack_audit_data {
0264 const char *function;
0265 char *subject;
0266 char *object;
0267 char *request;
0268 int result;
0269 };
0270
0271
0272
0273
0274
0275 struct smk_audit_info {
0276 #ifdef CONFIG_AUDIT
0277 struct common_audit_data a;
0278 struct smack_audit_data sad;
0279 #endif
0280 };
0281
0282
0283
0284
0285 int smk_access_entry(char *, char *, struct list_head *);
0286 int smk_access(struct smack_known *, struct smack_known *,
0287 int, struct smk_audit_info *);
0288 int smk_tskacc(struct task_smack *, struct smack_known *,
0289 u32, struct smk_audit_info *);
0290 int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
0291 struct smack_known *smack_from_secid(const u32);
0292 char *smk_parse_smack(const char *string, int len);
0293 int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
0294 struct smack_known *smk_import_entry(const char *, int);
0295 void smk_insert_entry(struct smack_known *skp);
0296 struct smack_known *smk_find_entry(const char *);
0297 bool smack_privileged(int cap);
0298 bool smack_privileged_cred(int cap, const struct cred *cred);
0299 void smk_destroy_label_list(struct list_head *list);
0300 int smack_populate_secattr(struct smack_known *skp);
0301
0302
0303
0304
0305 extern int smack_enabled __initdata;
0306 extern int smack_cipso_direct;
0307 extern int smack_cipso_mapped;
0308 extern struct smack_known *smack_net_ambient;
0309 extern struct smack_known *smack_syslog_label;
0310 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
0311 extern struct smack_known *smack_unconfined;
0312 #endif
0313 extern int smack_ptrace_rule;
0314 extern struct lsm_blob_sizes smack_blob_sizes;
0315
0316 extern struct smack_known smack_known_floor;
0317 extern struct smack_known smack_known_hat;
0318 extern struct smack_known smack_known_huh;
0319 extern struct smack_known smack_known_star;
0320 extern struct smack_known smack_known_web;
0321
0322 extern struct mutex smack_known_lock;
0323 extern struct list_head smack_known_list;
0324 extern struct list_head smk_net4addr_list;
0325 extern struct list_head smk_net6addr_list;
0326
0327 extern struct mutex smack_onlycap_lock;
0328 extern struct list_head smack_onlycap_list;
0329
0330 #define SMACK_HASH_SLOTS 16
0331 extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
0332 extern struct kmem_cache *smack_rule_cache;
0333
0334 static inline struct task_smack *smack_cred(const struct cred *cred)
0335 {
0336 return cred->security + smack_blob_sizes.lbs_cred;
0337 }
0338
0339 static inline struct smack_known **smack_file(const struct file *file)
0340 {
0341 return (struct smack_known **)(file->f_security +
0342 smack_blob_sizes.lbs_file);
0343 }
0344
0345 static inline struct inode_smack *smack_inode(const struct inode *inode)
0346 {
0347 return inode->i_security + smack_blob_sizes.lbs_inode;
0348 }
0349
0350 static inline struct smack_known **smack_msg_msg(const struct msg_msg *msg)
0351 {
0352 return msg->security + smack_blob_sizes.lbs_msg_msg;
0353 }
0354
0355 static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)
0356 {
0357 return ipc->security + smack_blob_sizes.lbs_ipc;
0358 }
0359
0360 static inline struct superblock_smack *smack_superblock(
0361 const struct super_block *superblock)
0362 {
0363 return superblock->s_security + smack_blob_sizes.lbs_superblock;
0364 }
0365
0366
0367
0368
0369 static inline int smk_inode_transmutable(const struct inode *isp)
0370 {
0371 struct inode_smack *sip = smack_inode(isp);
0372 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
0373 }
0374
0375
0376
0377
0378 static inline struct smack_known *smk_of_inode(const struct inode *isp)
0379 {
0380 struct inode_smack *sip = smack_inode(isp);
0381 return sip->smk_inode;
0382 }
0383
0384
0385
0386
0387 static inline struct smack_known *smk_of_task(const struct task_smack *tsp)
0388 {
0389 return tsp->smk_task;
0390 }
0391
0392 static inline struct smack_known *smk_of_task_struct_obj(
0393 const struct task_struct *t)
0394 {
0395 struct smack_known *skp;
0396 const struct cred *cred;
0397
0398 rcu_read_lock();
0399
0400 cred = __task_cred(t);
0401 skp = smk_of_task(smack_cred(cred));
0402
0403 rcu_read_unlock();
0404
0405 return skp;
0406 }
0407
0408
0409
0410
0411 static inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
0412 {
0413 return tsp->smk_forked;
0414 }
0415
0416
0417
0418
0419 static inline struct smack_known *smk_of_current(void)
0420 {
0421 return smk_of_task(smack_cred(current_cred()));
0422 }
0423
0424
0425
0426
0427 #define SMACK_AUDIT_DENIED 0x1
0428 #define SMACK_AUDIT_ACCEPT 0x2
0429 extern int log_policy;
0430
0431 void smack_log(char *subject_label, char *object_label,
0432 int request,
0433 int result, struct smk_audit_info *auditdata);
0434
0435 #ifdef CONFIG_AUDIT
0436
0437
0438
0439
0440
0441
0442 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
0443 char type)
0444 {
0445 memset(&a->sad, 0, sizeof(a->sad));
0446 a->a.type = type;
0447 a->a.smack_audit_data = &a->sad;
0448 a->a.smack_audit_data->function = func;
0449 }
0450
0451 static inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
0452 char type, struct lsm_network_audit *net)
0453 {
0454 smk_ad_init(a, func, type);
0455 memset(net, 0, sizeof(*net));
0456 a->a.u.net = net;
0457 }
0458
0459 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
0460 struct task_struct *t)
0461 {
0462 a->a.u.tsk = t;
0463 }
0464 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
0465 struct dentry *d)
0466 {
0467 a->a.u.dentry = d;
0468 }
0469 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
0470 struct inode *i)
0471 {
0472 a->a.u.inode = i;
0473 }
0474 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
0475 struct path p)
0476 {
0477 a->a.u.path = p;
0478 }
0479 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
0480 struct sock *sk)
0481 {
0482 a->a.u.net->sk = sk;
0483 }
0484
0485 #else
0486
0487 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
0488 char type)
0489 {
0490 }
0491 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
0492 struct task_struct *t)
0493 {
0494 }
0495 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
0496 struct dentry *d)
0497 {
0498 }
0499 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
0500 struct inode *i)
0501 {
0502 }
0503 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
0504 struct path p)
0505 {
0506 }
0507 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
0508 struct sock *sk)
0509 {
0510 }
0511 #endif
0512
0513 #endif