52
52
#define SMK_SENDING 2
53
53
54
54
#ifdef SMACK_IPV6_PORT_LABELING
55
+ DEFINE_MUTEX (smack_ipv6_lock );
55
56
static LIST_HEAD (smk_ipv6_port_list );
56
57
#endif
57
58
static struct kmem_cache * smack_inode_cache ;
@@ -347,8 +348,6 @@ static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
347
348
struct smack_rule * orp ;
348
349
int rc = 0 ;
349
350
350
- INIT_LIST_HEAD (nhead );
351
-
352
351
list_for_each_entry_rcu (orp , ohead , list ) {
353
352
nrp = kzalloc (sizeof (struct smack_rule ), gfp );
354
353
if (nrp == NULL ) {
@@ -375,8 +374,6 @@ static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
375
374
struct smack_known_list_elem * nklep ;
376
375
struct smack_known_list_elem * oklep ;
377
376
378
- INIT_LIST_HEAD (nhead );
379
-
380
377
list_for_each_entry (oklep , ohead , list ) {
381
378
nklep = kzalloc (sizeof (struct smack_known_list_elem ), gfp );
382
379
if (nklep == NULL ) {
@@ -1009,15 +1006,39 @@ static int smack_inode_alloc_security(struct inode *inode)
1009
1006
}
1010
1007
1011
1008
/**
1012
- * smack_inode_free_security - free an inode blob
1009
+ * smack_inode_free_rcu - Free inode_smack blob from cache
1010
+ * @head: the rcu_head for getting inode_smack pointer
1011
+ *
1012
+ * Call back function called from call_rcu() to free
1013
+ * the i_security blob pointer in inode
1014
+ */
1015
+ static void smack_inode_free_rcu (struct rcu_head * head )
1016
+ {
1017
+ struct inode_smack * issp ;
1018
+
1019
+ issp = container_of (head , struct inode_smack , smk_rcu );
1020
+ kmem_cache_free (smack_inode_cache , issp );
1021
+ }
1022
+
1023
+ /**
1024
+ * smack_inode_free_security - free an inode blob using call_rcu()
1013
1025
* @inode: the inode with a blob
1014
1026
*
1015
- * Clears the blob pointer in inode
1027
+ * Clears the blob pointer in inode using RCU
1016
1028
*/
1017
1029
static void smack_inode_free_security (struct inode * inode )
1018
1030
{
1019
- kmem_cache_free (smack_inode_cache , inode -> i_security );
1020
- inode -> i_security = NULL ;
1031
+ struct inode_smack * issp = inode -> i_security ;
1032
+
1033
+ /*
1034
+ * The inode may still be referenced in a path walk and
1035
+ * a call to smack_inode_permission() can be made
1036
+ * after smack_inode_free_security() is called.
1037
+ * To avoid race condition free the i_security via RCU
1038
+ * and leave the current inode->i_security pointer intact.
1039
+ * The inode will be freed after the RCU grace period too.
1040
+ */
1041
+ call_rcu (& issp -> smk_rcu , smack_inode_free_rcu );
1021
1042
}
1022
1043
1023
1044
/**
@@ -1626,6 +1647,9 @@ static int smack_file_ioctl(struct file *file, unsigned int cmd,
1626
1647
struct smk_audit_info ad ;
1627
1648
struct inode * inode = file_inode (file );
1628
1649
1650
+ if (unlikely (IS_PRIVATE (inode )))
1651
+ return 0 ;
1652
+
1629
1653
smk_ad_init (& ad , __func__ , LSM_AUDIT_DATA_PATH );
1630
1654
smk_ad_setfield_u_fs_path (& ad , file -> f_path );
1631
1655
@@ -1655,6 +1679,9 @@ static int smack_file_lock(struct file *file, unsigned int cmd)
1655
1679
int rc ;
1656
1680
struct inode * inode = file_inode (file );
1657
1681
1682
+ if (unlikely (IS_PRIVATE (inode )))
1683
+ return 0 ;
1684
+
1658
1685
smk_ad_init (& ad , __func__ , LSM_AUDIT_DATA_PATH );
1659
1686
smk_ad_setfield_u_fs_path (& ad , file -> f_path );
1660
1687
rc = smk_curacc (smk_of_inode (inode ), MAY_LOCK , & ad );
@@ -1681,6 +1708,9 @@ static int smack_file_fcntl(struct file *file, unsigned int cmd,
1681
1708
int rc = 0 ;
1682
1709
struct inode * inode = file_inode (file );
1683
1710
1711
+ if (unlikely (IS_PRIVATE (inode )))
1712
+ return 0 ;
1713
+
1684
1714
switch (cmd ) {
1685
1715
case F_GETLK :
1686
1716
break ;
@@ -1734,6 +1764,9 @@ static int smack_mmap_file(struct file *file,
1734
1764
if (file == NULL )
1735
1765
return 0 ;
1736
1766
1767
+ if (unlikely (IS_PRIVATE (file_inode (file ))))
1768
+ return 0 ;
1769
+
1737
1770
isp = file_inode (file )-> i_security ;
1738
1771
if (isp -> smk_mmap == NULL )
1739
1772
return 0 ;
@@ -1934,12 +1967,9 @@ static int smack_file_open(struct file *file, const struct cred *cred)
1934
1967
struct smk_audit_info ad ;
1935
1968
int rc ;
1936
1969
1937
- if (smack_privileged (CAP_MAC_OVERRIDE ))
1938
- return 0 ;
1939
-
1940
1970
smk_ad_init (& ad , __func__ , LSM_AUDIT_DATA_PATH );
1941
1971
smk_ad_setfield_u_fs_path (& ad , file -> f_path );
1942
- rc = smk_access (tsp -> smk_task , smk_of_inode (inode ), MAY_READ , & ad );
1972
+ rc = smk_tskacc (tsp , smk_of_inode (inode ), MAY_READ , & ad );
1943
1973
rc = smk_bu_credfile (cred , file , MAY_READ , rc );
1944
1974
1945
1975
return rc ;
@@ -2353,6 +2383,20 @@ static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2353
2383
*/
2354
2384
static void smack_sk_free_security (struct sock * sk )
2355
2385
{
2386
+ #ifdef SMACK_IPV6_PORT_LABELING
2387
+ struct smk_port_label * spp ;
2388
+
2389
+ if (sk -> sk_family == PF_INET6 ) {
2390
+ rcu_read_lock ();
2391
+ list_for_each_entry_rcu (spp , & smk_ipv6_port_list , list ) {
2392
+ if (spp -> smk_sock != sk )
2393
+ continue ;
2394
+ spp -> smk_can_reuse = 1 ;
2395
+ break ;
2396
+ }
2397
+ rcu_read_unlock ();
2398
+ }
2399
+ #endif
2356
2400
kfree (sk -> sk_security );
2357
2401
}
2358
2402
@@ -2603,17 +2647,20 @@ static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2603
2647
* on the bound socket. Take the changes to the port
2604
2648
* as well.
2605
2649
*/
2606
- list_for_each_entry (spp , & smk_ipv6_port_list , list ) {
2650
+ rcu_read_lock ();
2651
+ list_for_each_entry_rcu (spp , & smk_ipv6_port_list , list ) {
2607
2652
if (sk != spp -> smk_sock )
2608
2653
continue ;
2609
2654
spp -> smk_in = ssp -> smk_in ;
2610
2655
spp -> smk_out = ssp -> smk_out ;
2656
+ rcu_read_unlock ();
2611
2657
return ;
2612
2658
}
2613
2659
/*
2614
2660
* A NULL address is only used for updating existing
2615
2661
* bound entries. If there isn't one, it's OK.
2616
2662
*/
2663
+ rcu_read_unlock ();
2617
2664
return ;
2618
2665
}
2619
2666
@@ -2629,16 +2676,23 @@ static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2629
2676
* Look for an existing port list entry.
2630
2677
* This is an indication that a port is getting reused.
2631
2678
*/
2632
- list_for_each_entry (spp , & smk_ipv6_port_list , list ) {
2633
- if (spp -> smk_port != port )
2679
+ rcu_read_lock ();
2680
+ list_for_each_entry_rcu (spp , & smk_ipv6_port_list , list ) {
2681
+ if (spp -> smk_port != port || spp -> smk_sock_type != sock -> type )
2634
2682
continue ;
2683
+ if (spp -> smk_can_reuse != 1 ) {
2684
+ rcu_read_unlock ();
2685
+ return ;
2686
+ }
2635
2687
spp -> smk_port = port ;
2636
2688
spp -> smk_sock = sk ;
2637
2689
spp -> smk_in = ssp -> smk_in ;
2638
2690
spp -> smk_out = ssp -> smk_out ;
2691
+ spp -> smk_can_reuse = 0 ;
2692
+ rcu_read_unlock ();
2639
2693
return ;
2640
2694
}
2641
-
2695
+ rcu_read_unlock ();
2642
2696
/*
2643
2697
* A new port entry is required.
2644
2698
*/
@@ -2650,8 +2704,12 @@ static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2650
2704
spp -> smk_sock = sk ;
2651
2705
spp -> smk_in = ssp -> smk_in ;
2652
2706
spp -> smk_out = ssp -> smk_out ;
2707
+ spp -> smk_sock_type = sock -> type ;
2708
+ spp -> smk_can_reuse = 0 ;
2653
2709
2654
- list_add (& spp -> list , & smk_ipv6_port_list );
2710
+ mutex_lock (& smack_ipv6_lock );
2711
+ list_add_rcu (& spp -> list , & smk_ipv6_port_list );
2712
+ mutex_unlock (& smack_ipv6_lock );
2655
2713
return ;
2656
2714
}
2657
2715
@@ -2702,14 +2760,16 @@ static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2702
2760
return 0 ;
2703
2761
2704
2762
port = ntohs (address -> sin6_port );
2705
- list_for_each_entry (spp , & smk_ipv6_port_list , list ) {
2706
- if (spp -> smk_port != port )
2763
+ rcu_read_lock ();
2764
+ list_for_each_entry_rcu (spp , & smk_ipv6_port_list , list ) {
2765
+ if (spp -> smk_port != port || spp -> smk_sock_type != sk -> sk_type )
2707
2766
continue ;
2708
2767
object = spp -> smk_in ;
2709
2768
if (act == SMK_CONNECTING )
2710
2769
ssp -> smk_packet = spp -> smk_out ;
2711
2770
break ;
2712
2771
}
2772
+ rcu_read_unlock ();
2713
2773
2714
2774
return smk_ipv6_check (skp , object , address , act );
2715
2775
}
@@ -3438,6 +3498,13 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3438
3498
case PIPEFS_MAGIC :
3439
3499
isp -> smk_inode = smk_of_current ();
3440
3500
break ;
3501
+ case SOCKFS_MAGIC :
3502
+ /*
3503
+ * Socket access is controlled by the socket
3504
+ * structures associated with the task involved.
3505
+ */
3506
+ isp -> smk_inode = & smack_known_star ;
3507
+ break ;
3441
3508
default :
3442
3509
isp -> smk_inode = sbsp -> smk_root ;
3443
3510
break ;
@@ -3454,19 +3521,12 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3454
3521
*/
3455
3522
switch (sbp -> s_magic ) {
3456
3523
case SMACK_MAGIC :
3457
- case PIPEFS_MAGIC :
3458
- case SOCKFS_MAGIC :
3459
3524
case CGROUP_SUPER_MAGIC :
3460
3525
/*
3461
3526
* Casey says that it's a little embarrassing
3462
3527
* that the smack file system doesn't do
3463
3528
* extended attributes.
3464
3529
*
3465
- * Casey says pipes are easy (?)
3466
- *
3467
- * Socket access is controlled by the socket
3468
- * structures associated with the task involved.
3469
- *
3470
3530
* Cgroupfs is special
3471
3531
*/
3472
3532
final = & smack_known_star ;
@@ -3849,7 +3909,7 @@ static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3849
3909
* ambient value.
3850
3910
*/
3851
3911
rcu_read_lock ();
3852
- list_for_each_entry (skp , & smack_known_list , list ) {
3912
+ list_for_each_entry_rcu (skp , & smack_known_list , list ) {
3853
3913
if (sap -> attr .mls .lvl != skp -> smk_netlabel .attr .mls .lvl )
3854
3914
continue ;
3855
3915
/*
0 commit comments