12
12
#include <linux/module.h>
13
13
#include <linux/init.h>
14
14
#include <linux/kernel.h>
15
+ #include <linux/key.h>
15
16
#include <linux/bio.h>
16
17
#include <linux/blkdev.h>
17
18
#include <linux/mempool.h>
29
30
#include <crypto/md5.h>
30
31
#include <crypto/algapi.h>
31
32
#include <crypto/skcipher.h>
33
+ #include <keys/user-type.h>
32
34
33
35
#include <linux/device-mapper.h>
34
36
@@ -140,6 +142,7 @@ struct crypt_config {
140
142
141
143
char * cipher ;
142
144
char * cipher_string ;
145
+ char * key_string ;
143
146
144
147
const struct crypt_iv_operations * iv_gen_ops ;
145
148
union {
@@ -1484,22 +1487,134 @@ static int crypt_setkey(struct crypt_config *cc)
1484
1487
return err ;
1485
1488
}
1486
1489
1490
+ #ifdef CONFIG_KEYS
1491
+
1492
+ static int crypt_set_keyring_key (struct crypt_config * cc , const char * key_string )
1493
+ {
1494
+ char * new_key_string , * key_desc ;
1495
+ int ret ;
1496
+ struct key * key ;
1497
+ const struct user_key_payload * ukp ;
1498
+
1499
+ /* look for next ':' separating key_type from key_description */
1500
+ key_desc = strpbrk (key_string , ":" );
1501
+ if (!key_desc || key_desc == key_string || !strlen (key_desc + 1 ))
1502
+ return - EINVAL ;
1503
+
1504
+ if (strncmp (key_string , "logon:" , key_desc - key_string + 1 ) &&
1505
+ strncmp (key_string , "user:" , key_desc - key_string + 1 ))
1506
+ return - EINVAL ;
1507
+
1508
+ new_key_string = kstrdup (key_string , GFP_KERNEL );
1509
+ if (!new_key_string )
1510
+ return - ENOMEM ;
1511
+
1512
+ key = request_key (key_string [0 ] == 'l' ? & key_type_logon : & key_type_user ,
1513
+ key_desc + 1 , NULL );
1514
+ if (IS_ERR (key )) {
1515
+ kzfree (new_key_string );
1516
+ return PTR_ERR (key );
1517
+ }
1518
+
1519
+ rcu_read_lock ();
1520
+
1521
+ ukp = user_key_payload (key );
1522
+ if (!ukp ) {
1523
+ rcu_read_unlock ();
1524
+ key_put (key );
1525
+ kzfree (new_key_string );
1526
+ return - EKEYREVOKED ;
1527
+ }
1528
+
1529
+ if (cc -> key_size != ukp -> datalen ) {
1530
+ rcu_read_unlock ();
1531
+ key_put (key );
1532
+ kzfree (new_key_string );
1533
+ return - EINVAL ;
1534
+ }
1535
+
1536
+ memcpy (cc -> key , ukp -> data , cc -> key_size );
1537
+
1538
+ rcu_read_unlock ();
1539
+ key_put (key );
1540
+
1541
+ /* clear the flag since following operations may invalidate previously valid key */
1542
+ clear_bit (DM_CRYPT_KEY_VALID , & cc -> flags );
1543
+
1544
+ ret = crypt_setkey (cc );
1545
+
1546
+ /* wipe the kernel key payload copy in each case */
1547
+ memset (cc -> key , 0 , cc -> key_size * sizeof (u8 ));
1548
+
1549
+ if (!ret ) {
1550
+ set_bit (DM_CRYPT_KEY_VALID , & cc -> flags );
1551
+ kzfree (cc -> key_string );
1552
+ cc -> key_string = new_key_string ;
1553
+ } else
1554
+ kzfree (new_key_string );
1555
+
1556
+ return ret ;
1557
+ }
1558
+
1559
+ static int get_key_size (char * * key_string )
1560
+ {
1561
+ char * colon , dummy ;
1562
+ int ret ;
1563
+
1564
+ if (* key_string [0 ] != ':' )
1565
+ return strlen (* key_string ) >> 1 ;
1566
+
1567
+ /* look for next ':' in key string */
1568
+ colon = strpbrk (* key_string + 1 , ":" );
1569
+ if (!colon )
1570
+ return - EINVAL ;
1571
+
1572
+ if (sscanf (* key_string + 1 , "%u%c" , & ret , & dummy ) != 2 || dummy != ':' )
1573
+ return - EINVAL ;
1574
+
1575
+ * key_string = colon ;
1576
+
1577
+ /* remaining key string should be :<logon|user>:<key_desc> */
1578
+
1579
+ return ret ;
1580
+ }
1581
+
1582
+ #else
1583
+
1584
+ static int crypt_set_keyring_key (struct crypt_config * cc , const char * key_string )
1585
+ {
1586
+ return - EINVAL ;
1587
+ }
1588
+
1589
+ static int get_key_size (char * * key_string )
1590
+ {
1591
+ return (* key_string [0 ] == ':' ) ? - EINVAL : strlen (* key_string ) >> 1 ;
1592
+ }
1593
+
1594
+ #endif
1595
+
1487
1596
static int crypt_set_key (struct crypt_config * cc , char * key )
1488
1597
{
1489
1598
int r = - EINVAL ;
1490
1599
int key_string_len = strlen (key );
1491
1600
1492
- /* The key size may not be changed. */
1493
- if (cc -> key_size != (key_string_len >> 1 ))
1494
- goto out ;
1495
-
1496
1601
/* Hyphen (which gives a key_size of zero) means there is no key. */
1497
1602
if (!cc -> key_size && strcmp (key , "-" ))
1498
1603
goto out ;
1499
1604
1605
+ /* ':' means the key is in kernel keyring, short-circuit normal key processing */
1606
+ if (key [0 ] == ':' ) {
1607
+ r = crypt_set_keyring_key (cc , key + 1 );
1608
+ goto out ;
1609
+ }
1610
+
1500
1611
/* clear the flag since following operations may invalidate previously valid key */
1501
1612
clear_bit (DM_CRYPT_KEY_VALID , & cc -> flags );
1502
1613
1614
+ /* wipe references to any kernel keyring key */
1615
+ kzfree (cc -> key_string );
1616
+ cc -> key_string = NULL ;
1617
+
1503
1618
if (cc -> key_size && crypt_decode_key (cc -> key , key , cc -> key_size ) < 0 )
1504
1619
goto out ;
1505
1620
@@ -1518,6 +1633,8 @@ static int crypt_wipe_key(struct crypt_config *cc)
1518
1633
{
1519
1634
clear_bit (DM_CRYPT_KEY_VALID , & cc -> flags );
1520
1635
memset (& cc -> key , 0 , cc -> key_size * sizeof (u8 ));
1636
+ kzfree (cc -> key_string );
1637
+ cc -> key_string = NULL ;
1521
1638
1522
1639
return crypt_setkey (cc );
1523
1640
}
@@ -1555,6 +1672,7 @@ static void crypt_dtr(struct dm_target *ti)
1555
1672
1556
1673
kzfree (cc -> cipher );
1557
1674
kzfree (cc -> cipher_string );
1675
+ kzfree (cc -> key_string );
1558
1676
1559
1677
/* Must zero key material before freeing */
1560
1678
kzfree (cc );
@@ -1723,12 +1841,13 @@ static int crypt_ctr_cipher(struct dm_target *ti,
1723
1841
1724
1842
/*
1725
1843
* Construct an encryption mapping:
1726
- * <cipher> <key> <iv_offset> <dev_path> <start>
1844
+ * <cipher> [ <key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
1727
1845
*/
1728
1846
static int crypt_ctr (struct dm_target * ti , unsigned int argc , char * * argv )
1729
1847
{
1730
1848
struct crypt_config * cc ;
1731
- unsigned int key_size , opt_params ;
1849
+ int key_size ;
1850
+ unsigned int opt_params ;
1732
1851
unsigned long long tmpll ;
1733
1852
int ret ;
1734
1853
size_t iv_size_padding ;
@@ -1745,7 +1864,11 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1745
1864
return - EINVAL ;
1746
1865
}
1747
1866
1748
- key_size = strlen (argv [1 ]) >> 1 ;
1867
+ key_size = get_key_size (& argv [1 ]);
1868
+ if (key_size < 0 ) {
1869
+ ti -> error = "Cannot parse key size" ;
1870
+ return - EINVAL ;
1871
+ }
1749
1872
1750
1873
cc = kzalloc (sizeof (* cc ) + key_size * sizeof (u8 ), GFP_KERNEL );
1751
1874
if (!cc ) {
@@ -1952,10 +2075,13 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
1952
2075
case STATUSTYPE_TABLE :
1953
2076
DMEMIT ("%s " , cc -> cipher_string );
1954
2077
1955
- if (cc -> key_size > 0 )
1956
- for (i = 0 ; i < cc -> key_size ; i ++ )
1957
- DMEMIT ("%02x" , cc -> key [i ]);
1958
- else
2078
+ if (cc -> key_size > 0 ) {
2079
+ if (cc -> key_string )
2080
+ DMEMIT (":%u:%s" , cc -> key_size , cc -> key_string );
2081
+ else
2082
+ for (i = 0 ; i < cc -> key_size ; i ++ )
2083
+ DMEMIT ("%02x" , cc -> key [i ]);
2084
+ } else
1959
2085
DMEMIT ("-" );
1960
2086
1961
2087
DMEMIT (" %llu %s %llu" , (unsigned long long )cc -> iv_offset ,
@@ -2011,7 +2137,7 @@ static void crypt_resume(struct dm_target *ti)
2011
2137
static int crypt_message (struct dm_target * ti , unsigned argc , char * * argv )
2012
2138
{
2013
2139
struct crypt_config * cc = ti -> private ;
2014
- int ret = - EINVAL ;
2140
+ int key_size , ret = - EINVAL ;
2015
2141
2016
2142
if (argc < 2 )
2017
2143
goto error ;
@@ -2022,6 +2148,13 @@ static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2022
2148
return - EINVAL ;
2023
2149
}
2024
2150
if (argc == 3 && !strcasecmp (argv [1 ], "set" )) {
2151
+ /* The key size may not be changed. */
2152
+ key_size = get_key_size (& argv [2 ]);
2153
+ if (key_size < 0 || cc -> key_size != key_size ) {
2154
+ memset (argv [2 ], '0' , strlen (argv [2 ]));
2155
+ return - EINVAL ;
2156
+ }
2157
+
2025
2158
ret = crypt_set_key (cc , argv [2 ]);
2026
2159
if (ret )
2027
2160
return ret ;
@@ -2065,7 +2198,7 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2065
2198
2066
2199
static struct target_type crypt_target = {
2067
2200
.name = "crypt" ,
2068
- .version = {1 , 14 , 1 },
2201
+ .version = {1 , 15 , 0 },
2069
2202
.module = THIS_MODULE ,
2070
2203
.ctr = crypt_ctr ,
2071
2204
.dtr = crypt_dtr ,
0 commit comments