@@ -1178,6 +1178,12 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
1178
1178
static const struct inode_operations shmem_symlink_inode_operations ;
1179
1179
static const struct inode_operations shmem_short_symlink_operations ;
1180
1180
1181
+ #ifdef CONFIG_TMPFS_XATTR
1182
+ static int shmem_initxattrs (struct inode * , const struct xattr * , void * );
1183
+ #else
1184
+ #define shmem_initxattrs NULL
1185
+ #endif
1186
+
1181
1187
static int
1182
1188
shmem_write_begin (struct file * file , struct address_space * mapping ,
1183
1189
loff_t pos , unsigned len , unsigned flags ,
@@ -1490,7 +1496,7 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1490
1496
if (inode ) {
1491
1497
error = security_inode_init_security (inode , dir ,
1492
1498
& dentry -> d_name ,
1493
- NULL , NULL );
1499
+ shmem_initxattrs , NULL );
1494
1500
if (error ) {
1495
1501
if (error != - EOPNOTSUPP ) {
1496
1502
iput (inode );
@@ -1630,7 +1636,7 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
1630
1636
return - ENOSPC ;
1631
1637
1632
1638
error = security_inode_init_security (inode , dir , & dentry -> d_name ,
1633
- NULL , NULL );
1639
+ shmem_initxattrs , NULL );
1634
1640
if (error ) {
1635
1641
if (error != - EOPNOTSUPP ) {
1636
1642
iput (inode );
@@ -1704,6 +1710,66 @@ static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *co
1704
1710
* filesystem level, though.
1705
1711
*/
1706
1712
1713
+ /*
1714
+ * Allocate new xattr and copy in the value; but leave the name to callers.
1715
+ */
1716
+ static struct shmem_xattr * shmem_xattr_alloc (const void * value , size_t size )
1717
+ {
1718
+ struct shmem_xattr * new_xattr ;
1719
+ size_t len ;
1720
+
1721
+ /* wrap around? */
1722
+ len = sizeof (* new_xattr ) + size ;
1723
+ if (len <= sizeof (* new_xattr ))
1724
+ return NULL ;
1725
+
1726
+ new_xattr = kmalloc (len , GFP_KERNEL );
1727
+ if (!new_xattr )
1728
+ return NULL ;
1729
+
1730
+ new_xattr -> size = size ;
1731
+ memcpy (new_xattr -> value , value , size );
1732
+ return new_xattr ;
1733
+ }
1734
+
1735
+ /*
1736
+ * Callback for security_inode_init_security() for acquiring xattrs.
1737
+ */
1738
+ static int shmem_initxattrs (struct inode * inode ,
1739
+ const struct xattr * xattr_array ,
1740
+ void * fs_info )
1741
+ {
1742
+ struct shmem_inode_info * info = SHMEM_I (inode );
1743
+ const struct xattr * xattr ;
1744
+ struct shmem_xattr * new_xattr ;
1745
+ size_t len ;
1746
+
1747
+ for (xattr = xattr_array ; xattr -> name != NULL ; xattr ++ ) {
1748
+ new_xattr = shmem_xattr_alloc (xattr -> value , xattr -> value_len );
1749
+ if (!new_xattr )
1750
+ return - ENOMEM ;
1751
+
1752
+ len = strlen (xattr -> name ) + 1 ;
1753
+ new_xattr -> name = kmalloc (XATTR_SECURITY_PREFIX_LEN + len ,
1754
+ GFP_KERNEL );
1755
+ if (!new_xattr -> name ) {
1756
+ kfree (new_xattr );
1757
+ return - ENOMEM ;
1758
+ }
1759
+
1760
+ memcpy (new_xattr -> name , XATTR_SECURITY_PREFIX ,
1761
+ XATTR_SECURITY_PREFIX_LEN );
1762
+ memcpy (new_xattr -> name + XATTR_SECURITY_PREFIX_LEN ,
1763
+ xattr -> name , len );
1764
+
1765
+ spin_lock (& info -> lock );
1766
+ list_add (& new_xattr -> list , & info -> xattr_list );
1767
+ spin_unlock (& info -> lock );
1768
+ }
1769
+
1770
+ return 0 ;
1771
+ }
1772
+
1707
1773
static int shmem_xattr_get (struct dentry * dentry , const char * name ,
1708
1774
void * buffer , size_t size )
1709
1775
{
@@ -1731,24 +1797,17 @@ static int shmem_xattr_get(struct dentry *dentry, const char *name,
1731
1797
return ret ;
1732
1798
}
1733
1799
1734
- static int shmem_xattr_set (struct dentry * dentry , const char * name ,
1800
+ static int shmem_xattr_set (struct inode * inode , const char * name ,
1735
1801
const void * value , size_t size , int flags )
1736
1802
{
1737
- struct inode * inode = dentry -> d_inode ;
1738
1803
struct shmem_inode_info * info = SHMEM_I (inode );
1739
1804
struct shmem_xattr * xattr ;
1740
1805
struct shmem_xattr * new_xattr = NULL ;
1741
- size_t len ;
1742
1806
int err = 0 ;
1743
1807
1744
1808
/* value == NULL means remove */
1745
1809
if (value ) {
1746
- /* wrap around? */
1747
- len = sizeof (* new_xattr ) + size ;
1748
- if (len <= sizeof (* new_xattr ))
1749
- return - ENOMEM ;
1750
-
1751
- new_xattr = kmalloc (len , GFP_KERNEL );
1810
+ new_xattr = shmem_xattr_alloc (value , size );
1752
1811
if (!new_xattr )
1753
1812
return - ENOMEM ;
1754
1813
@@ -1757,9 +1816,6 @@ static int shmem_xattr_set(struct dentry *dentry, const char *name,
1757
1816
kfree (new_xattr );
1758
1817
return - ENOMEM ;
1759
1818
}
1760
-
1761
- new_xattr -> size = size ;
1762
- memcpy (new_xattr -> value , value , size );
1763
1819
}
1764
1820
1765
1821
spin_lock (& info -> lock );
@@ -1858,7 +1914,7 @@ static int shmem_setxattr(struct dentry *dentry, const char *name,
1858
1914
if (size == 0 )
1859
1915
value = "" ; /* empty EA, do not remove */
1860
1916
1861
- return shmem_xattr_set (dentry , name , value , size , flags );
1917
+ return shmem_xattr_set (dentry -> d_inode , name , value , size , flags );
1862
1918
1863
1919
}
1864
1920
@@ -1878,7 +1934,7 @@ static int shmem_removexattr(struct dentry *dentry, const char *name)
1878
1934
if (err )
1879
1935
return err ;
1880
1936
1881
- return shmem_xattr_set (dentry , name , NULL , 0 , XATTR_REPLACE );
1937
+ return shmem_xattr_set (dentry -> d_inode , name , NULL , 0 , XATTR_REPLACE );
1882
1938
}
1883
1939
1884
1940
static bool xattr_is_trusted (const char * name )
0 commit comments