33 *
44 * Copyright (C) 1997-2004 Erez Zadok
55 * Copyright (C) 2001-2004 Stony Brook University
6- * Copyright (C) 2004-2006 International Business Machines Corp.
6+ * Copyright (C) 2004-2007 International Business Machines Corp.
77 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
88 * Michael C. Thompson <mcthomps@us.ibm.com>
99 *
@@ -863,7 +863,10 @@ void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
863863 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE ;
864864 } else
865865 crypt_stat -> header_extent_size = PAGE_CACHE_SIZE ;
866- crypt_stat -> num_header_extents_at_front = 1 ;
866+ if (crypt_stat -> flags & ECRYPTFS_METADATA_IN_XATTR )
867+ crypt_stat -> num_header_extents_at_front = 0 ;
868+ else
869+ crypt_stat -> num_header_extents_at_front = 1 ;
867870}
868871
869872/**
@@ -1021,7 +1024,7 @@ int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
10211024 *
10221025 * Returns one if marker found; zero if not found
10231026 */
1024- int contains_ecryptfs_marker (char * data )
1027+ static int contains_ecryptfs_marker (char * data )
10251028{
10261029 u32 m_1 , m_2 ;
10271030
@@ -1047,7 +1050,8 @@ struct ecryptfs_flag_map_elem {
10471050/* Add support for additional flags by adding elements here. */
10481051static struct ecryptfs_flag_map_elem ecryptfs_flag_map [] = {
10491052 {0x00000001 , ECRYPTFS_ENABLE_HMAC },
1050- {0x00000002 , ECRYPTFS_ENCRYPTED }
1053+ {0x00000002 , ECRYPTFS_ENCRYPTED },
1054+ {0x00000004 , ECRYPTFS_METADATA_IN_XATTR }
10511055};
10521056
10531057/**
@@ -1207,8 +1211,8 @@ int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
12071211 *
12081212 * Returns zero on success; non-zero otherwise
12091213 */
1210- int ecryptfs_read_header_region (char * data , struct dentry * dentry ,
1211- struct vfsmount * mnt )
1214+ static int ecryptfs_read_header_region (char * data , struct dentry * dentry ,
1215+ struct vfsmount * mnt )
12121216{
12131217 struct file * lower_file ;
12141218 mm_segment_t oldfs ;
@@ -1237,6 +1241,21 @@ int ecryptfs_read_header_region(char *data, struct dentry *dentry,
12371241 return rc ;
12381242}
12391243
1244+ int ecryptfs_read_and_validate_header_region (char * data , struct dentry * dentry ,
1245+ struct vfsmount * mnt )
1246+ {
1247+ int rc ;
1248+
1249+ rc = ecryptfs_read_header_region (data , dentry , mnt );
1250+ if (rc )
1251+ goto out ;
1252+ if (!contains_ecryptfs_marker (data + ECRYPTFS_FILE_SIZE_BYTES ))
1253+ rc = - EINVAL ;
1254+ out :
1255+ return rc ;
1256+ }
1257+
1258+
12401259static void
12411260write_header_metadata (char * virt , struct ecryptfs_crypt_stat * crypt_stat ,
12421261 size_t * written )
@@ -1288,9 +1307,9 @@ struct kmem_cache *ecryptfs_header_cache_2;
12881307 *
12891308 * Returns zero on success
12901309 */
1291- int ecryptfs_write_headers_virt (char * page_virt ,
1292- struct ecryptfs_crypt_stat * crypt_stat ,
1293- struct dentry * ecryptfs_dentry )
1310+ static int ecryptfs_write_headers_virt (char * page_virt , size_t * size ,
1311+ struct ecryptfs_crypt_stat * crypt_stat ,
1312+ struct dentry * ecryptfs_dentry )
12941313{
12951314 int rc ;
12961315 size_t written ;
@@ -1309,11 +1328,53 @@ int ecryptfs_write_headers_virt(char *page_virt,
13091328 if (rc )
13101329 ecryptfs_printk (KERN_WARNING , "Error generating key packet "
13111330 "set; rc = [%d]\n" , rc );
1331+ if (size ) {
1332+ offset += written ;
1333+ * size = offset ;
1334+ }
1335+ return rc ;
1336+ }
1337+
1338+ static int ecryptfs_write_metadata_to_contents (struct ecryptfs_crypt_stat * crypt_stat ,
1339+ struct file * lower_file ,
1340+ char * page_virt )
1341+ {
1342+ mm_segment_t oldfs ;
1343+ int current_header_page ;
1344+ int header_pages ;
1345+
1346+ lower_file -> f_pos = 0 ;
1347+ oldfs = get_fs ();
1348+ set_fs (get_ds ());
1349+ lower_file -> f_op -> write (lower_file , (char __user * )page_virt ,
1350+ PAGE_CACHE_SIZE , & lower_file -> f_pos );
1351+ header_pages = ((crypt_stat -> header_extent_size
1352+ * crypt_stat -> num_header_extents_at_front )
1353+ / PAGE_CACHE_SIZE );
1354+ memset (page_virt , 0 , PAGE_CACHE_SIZE );
1355+ current_header_page = 1 ;
1356+ while (current_header_page < header_pages ) {
1357+ lower_file -> f_op -> write (lower_file , (char __user * )page_virt ,
1358+ PAGE_CACHE_SIZE , & lower_file -> f_pos );
1359+ current_header_page ++ ;
1360+ }
1361+ set_fs (oldfs );
1362+ return 0 ;
1363+ }
1364+
1365+ static int ecryptfs_write_metadata_to_xattr (struct dentry * ecryptfs_dentry ,
1366+ struct ecryptfs_crypt_stat * crypt_stat ,
1367+ char * page_virt , size_t size )
1368+ {
1369+ int rc ;
1370+
1371+ rc = ecryptfs_setxattr (ecryptfs_dentry , ECRYPTFS_XATTR_NAME , page_virt ,
1372+ size , 0 );
13121373 return rc ;
13131374}
13141375
13151376/**
1316- * ecryptfs_write_headers
1377+ * ecryptfs_write_metadata
13171378 * @lower_file: The lower file struct, which was returned from dentry_open
13181379 *
13191380 * Write the file headers out. This will likely involve a userspace
@@ -1324,14 +1385,12 @@ int ecryptfs_write_headers_virt(char *page_virt,
13241385 *
13251386 * Returns zero on success; non-zero on error
13261387 */
1327- int ecryptfs_write_headers (struct dentry * ecryptfs_dentry ,
1328- struct file * lower_file )
1388+ int ecryptfs_write_metadata (struct dentry * ecryptfs_dentry ,
1389+ struct file * lower_file )
13291390{
1330- mm_segment_t oldfs ;
13311391 struct ecryptfs_crypt_stat * crypt_stat ;
13321392 char * page_virt ;
1333- int current_header_page ;
1334- int header_pages ;
1393+ size_t size ;
13351394 int rc = 0 ;
13361395
13371396 crypt_stat = & ecryptfs_inode_to_private (
@@ -1358,48 +1417,36 @@ int ecryptfs_write_headers(struct dentry *ecryptfs_dentry,
13581417 rc = - ENOMEM ;
13591418 goto out ;
13601419 }
1361-
1362- rc = ecryptfs_write_headers_virt (page_virt , crypt_stat ,
1363- ecryptfs_dentry );
1420+ rc = ecryptfs_write_headers_virt (page_virt , & size , crypt_stat ,
1421+ ecryptfs_dentry );
13641422 if (unlikely (rc )) {
13651423 ecryptfs_printk (KERN_ERR , "Error whilst writing headers\n" );
13661424 memset (page_virt , 0 , PAGE_CACHE_SIZE );
13671425 goto out_free ;
13681426 }
1369- ecryptfs_printk (KERN_DEBUG ,
1370- "Writing key packet set to underlying file\n" );
1371- lower_file -> f_pos = 0 ;
1372- oldfs = get_fs ();
1373- set_fs (get_ds ());
1374- ecryptfs_printk (KERN_DEBUG , "Calling lower_file->f_op->"
1375- "write() w/ header page; lower_file->f_pos = "
1376- "[0x%.16x]\n" , lower_file -> f_pos );
1377- lower_file -> f_op -> write (lower_file , (char __user * )page_virt ,
1378- PAGE_CACHE_SIZE , & lower_file -> f_pos );
1379- header_pages = ((crypt_stat -> header_extent_size
1380- * crypt_stat -> num_header_extents_at_front )
1381- / PAGE_CACHE_SIZE );
1382- memset (page_virt , 0 , PAGE_CACHE_SIZE );
1383- current_header_page = 1 ;
1384- while (current_header_page < header_pages ) {
1385- ecryptfs_printk (KERN_DEBUG , "Calling lower_file->f_op->"
1386- "write() w/ zero'd page; lower_file->f_pos = "
1387- "[0x%.16x]\n" , lower_file -> f_pos );
1388- lower_file -> f_op -> write (lower_file , (char __user * )page_virt ,
1389- PAGE_CACHE_SIZE , & lower_file -> f_pos );
1390- current_header_page ++ ;
1427+ if (crypt_stat -> flags & ECRYPTFS_METADATA_IN_XATTR )
1428+ rc = ecryptfs_write_metadata_to_xattr (ecryptfs_dentry ,
1429+ crypt_stat , page_virt ,
1430+ size );
1431+ else
1432+ rc = ecryptfs_write_metadata_to_contents (crypt_stat , lower_file ,
1433+ page_virt );
1434+ if (rc ) {
1435+ printk (KERN_ERR "Error writing metadata out to lower file; "
1436+ "rc = [%d]\n" , rc );
1437+ goto out_free ;
13911438 }
1392- set_fs (oldfs );
1393- ecryptfs_printk (KERN_DEBUG ,
1394- "Done writing key packet set to underlying file.\n" );
13951439out_free :
13961440 kmem_cache_free (ecryptfs_header_cache_0 , page_virt );
13971441out :
13981442 return rc ;
13991443}
14001444
1445+ #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1446+ #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
14011447static int parse_header_metadata (struct ecryptfs_crypt_stat * crypt_stat ,
1402- char * virt , int * bytes_read )
1448+ char * virt , int * bytes_read ,
1449+ int validate_header_size )
14031450{
14041451 int rc = 0 ;
14051452 u32 header_extent_size ;
@@ -1414,9 +1461,10 @@ static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
14141461 crypt_stat -> num_header_extents_at_front =
14151462 (int )num_header_extents_at_front ;
14161463 (* bytes_read ) = 6 ;
1417- if ((crypt_stat -> header_extent_size
1418- * crypt_stat -> num_header_extents_at_front )
1419- < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE ) {
1464+ if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE )
1465+ && ((crypt_stat -> header_extent_size
1466+ * crypt_stat -> num_header_extents_at_front )
1467+ < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE )) {
14201468 rc = - EINVAL ;
14211469 ecryptfs_printk (KERN_WARNING , "Invalid header extent size: "
14221470 "[%d]\n" , crypt_stat -> header_extent_size );
@@ -1447,7 +1495,8 @@ static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
14471495 */
14481496static int ecryptfs_read_headers_virt (char * page_virt ,
14491497 struct ecryptfs_crypt_stat * crypt_stat ,
1450- struct dentry * ecryptfs_dentry )
1498+ struct dentry * ecryptfs_dentry ,
1499+ int validate_header_size )
14511500{
14521501 int rc = 0 ;
14531502 int offset ;
@@ -1481,7 +1530,7 @@ static int ecryptfs_read_headers_virt(char *page_virt,
14811530 offset += bytes_read ;
14821531 if (crypt_stat -> file_version >= 1 ) {
14831532 rc = parse_header_metadata (crypt_stat , (page_virt + offset ),
1484- & bytes_read );
1533+ & bytes_read , validate_header_size );
14851534 if (rc ) {
14861535 ecryptfs_printk (KERN_WARNING , "Error reading header "
14871536 "metadata; rc = [%d]\n" , rc );
@@ -1496,12 +1545,60 @@ static int ecryptfs_read_headers_virt(char *page_virt,
14961545}
14971546
14981547/**
1499- * ecryptfs_read_headers
1548+ * ecryptfs_read_xattr_region
1549+ *
1550+ * Attempts to read the crypto metadata from the extended attribute
1551+ * region of the lower file.
1552+ */
1553+ int ecryptfs_read_xattr_region (char * page_virt , struct dentry * ecryptfs_dentry )
1554+ {
1555+ ssize_t size ;
1556+ int rc = 0 ;
1557+
1558+ size = ecryptfs_getxattr (ecryptfs_dentry , ECRYPTFS_XATTR_NAME ,
1559+ page_virt , ECRYPTFS_DEFAULT_EXTENT_SIZE );
1560+ if (size < 0 ) {
1561+ printk (KERN_DEBUG "Error attempting to read the [%s] "
1562+ "xattr from the lower file; return value = [%zd]\n" ,
1563+ ECRYPTFS_XATTR_NAME , size );
1564+ rc = - EINVAL ;
1565+ goto out ;
1566+ }
1567+ out :
1568+ return rc ;
1569+ }
1570+
1571+ int ecryptfs_read_and_validate_xattr_region (char * page_virt ,
1572+ struct dentry * ecryptfs_dentry )
1573+ {
1574+ int rc ;
1575+
1576+ rc = ecryptfs_read_xattr_region (page_virt , ecryptfs_dentry );
1577+ if (rc )
1578+ goto out ;
1579+ if (!contains_ecryptfs_marker (page_virt + ECRYPTFS_FILE_SIZE_BYTES )) {
1580+ printk (KERN_WARNING "Valid data found in [%s] xattr, but "
1581+ "the marker is invalid\n" , ECRYPTFS_XATTR_NAME );
1582+ rc = - EINVAL ;
1583+ }
1584+ out :
1585+ return rc ;
1586+ }
1587+
1588+ /**
1589+ * ecryptfs_read_metadata
1590+ *
1591+ * Common entry point for reading file metadata. From here, we could
1592+ * retrieve the header information from the header region of the file,
1593+ * the xattr region of the file, or some other repostory that is
1594+ * stored separately from the file itself. The current implementation
1595+ * supports retrieving the metadata information from the file contents
1596+ * and from the xattr region.
15001597 *
15011598 * Returns zero if valid headers found and parsed; non-zero otherwise
15021599 */
1503- int ecryptfs_read_headers (struct dentry * ecryptfs_dentry ,
1504- struct file * lower_file )
1600+ int ecryptfs_read_metadata (struct dentry * ecryptfs_dentry ,
1601+ struct file * lower_file )
15051602{
15061603 int rc = 0 ;
15071604 char * page_virt = NULL ;
@@ -1530,11 +1627,36 @@ int ecryptfs_read_headers(struct dentry *ecryptfs_dentry,
15301627 goto out ;
15311628 }
15321629 rc = ecryptfs_read_headers_virt (page_virt , crypt_stat ,
1533- ecryptfs_dentry );
1630+ ecryptfs_dentry ,
1631+ ECRYPTFS_VALIDATE_HEADER_SIZE );
15341632 if (rc ) {
1535- ecryptfs_printk (KERN_DEBUG , "Valid eCryptfs headers not "
1536- "found\n" );
1537- rc = - EINVAL ;
1633+ rc = ecryptfs_read_xattr_region (page_virt ,
1634+ ecryptfs_dentry );
1635+ if (rc ) {
1636+ printk (KERN_DEBUG "Valid eCryptfs headers not found in "
1637+ "file header region or xattr region\n" );
1638+ rc = - EINVAL ;
1639+ goto out ;
1640+ }
1641+ rc = ecryptfs_read_headers_virt (page_virt , crypt_stat ,
1642+ ecryptfs_dentry ,
1643+ ECRYPTFS_DONT_VALIDATE_HEADER_SIZE );
1644+ if (rc ) {
1645+ printk (KERN_DEBUG "Valid eCryptfs headers not found in "
1646+ "file xattr region either\n" );
1647+ rc = - EINVAL ;
1648+ }
1649+ if (crypt_stat -> mount_crypt_stat -> flags
1650+ & ECRYPTFS_XATTR_METADATA_ENABLED ) {
1651+ crypt_stat -> flags |= ECRYPTFS_METADATA_IN_XATTR ;
1652+ } else {
1653+ printk (KERN_WARNING "Attempt to access file with "
1654+ "crypto metadata only in the extended attribute "
1655+ "region, but eCryptfs was mounted without "
1656+ "xattr support enabled. eCryptfs will not treat "
1657+ "this like an encrypted file.\n" );
1658+ rc = - EINVAL ;
1659+ }
15381660 }
15391661out :
15401662 if (page_virt ) {
0 commit comments