Add --enable-fips option. Request new gcrypt if set.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
index 0a7edd6..7510dec 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <stdio.h>
@@ -119,6 +119,9 @@ static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
        if (crypt_maj >= 1 && crypt_min >= 8)
                _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
 
+       if (crypt_maj >= 1 && crypt_min >= 11)
+               _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
+
        /* Repeat test if dm-crypt is not present */
        if (crypt_maj > 0)
                _dm_crypt_checked = 1;
@@ -238,25 +241,45 @@ static void hex_key(char *hexkey, size_t key_size, const char *key)
                sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
 }
 
-static char *get_params(const char *device, uint64_t skip, uint64_t offset,
-                       const char *cipher, struct volume_key *vk)
+static char *get_params(struct crypt_dm_active_device *dmd)
 {
-       char *params;
-       char *hexkey;
+       int r, max_size, null_cipher = 0;
+       char *params, *hexkey;
+       const char *features = "";
+
+       if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
+               if (dm_flags() & DM_DISCARDS_SUPPORTED) {
+                       features = " 1 allow_discards";
+                       log_dbg("Discard/TRIM is allowed.");
+               } else
+                       log_dbg("Discard/TRIM is not supported by the kernel.");
+       }
+
+       if (!strncmp(dmd->cipher, "cipher_null-", 12))
+               null_cipher = 1;
 
-       hexkey = crypt_safe_alloc(vk->keylength * 2 + 1);
+       hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->vk->keylength * 2 + 1));
        if (!hexkey)
                return NULL;
 
-       hex_key(hexkey, vk->keylength, vk->key);
+       if (null_cipher)
+               strncpy(hexkey, "-", 2);
+       else
+               hex_key(hexkey, dmd->vk->keylength, dmd->vk->key);
 
-       params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
+       max_size = strlen(hexkey) + strlen(dmd->cipher) +
+                  strlen(dmd->device) + strlen(features) + 64;
+       params = crypt_safe_alloc(max_size);
        if (!params)
                goto out;
 
-       sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
-               cipher, hexkey, skip, device, offset);
-
+       r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
+                    dmd->cipher, hexkey, dmd->iv_offset, dmd->device,
+                    dmd->offset, features);
+       if (r < 0 || r >= max_size) {
+               crypt_safe_free(params);
+               params = NULL;
+       }
 out:
        crypt_safe_free(hexkey);
        return params;
@@ -278,6 +301,11 @@ static int _dm_simple(int task, const char *name, int udev_wait)
        if (name && !dm_task_set_name(dmt, name))
                goto out;
 
+#if HAVE_DECL_DM_TASK_RETRY_REMOVE
+       /* Used only in DM_DEVICE_REMOVE */
+       if (name && !dm_task_retry_remove(dmt))
+               goto out;
+#endif
        if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
                goto out;
 
@@ -403,19 +431,17 @@ int dm_create_device(const char *name,
        struct dm_task *dmt = NULL;
        struct dm_info dmi;
        char *params = NULL;
-       char *error = NULL;
        char dev_uuid[DM_UUID_LEN] = {0};
        int r = -EINVAL;
        uint32_t read_ahead = 0;
        uint32_t cookie = 0;
        uint16_t udev_flags = 0;
 
-       params = get_params(dmd->device, dmd->iv_offset, dmd->offset,
-                           dmd->cipher, dmd->vk);
+       params = get_params(dmd);
        if (!params)
                goto out_no_removal;
 
-       if (type && !strncmp(type, "TEMP", 4))
+       if (dmd->flags & CRYPT_ACTIVATE_PRIVATE)
                udev_flags = CRYPT_TEMP_UDEV_FLAGS;
 
        /* All devices must have DM_UUID, only resize on old device is exception */
@@ -481,18 +507,9 @@ out:
                cookie = 0;
        }
 
-       if (r < 0 && !reload) {
-               if (get_error())
-                       error = strdup(get_error());
-
+       if (r < 0 && !reload)
                dm_remove_device(name, 0, 0);
 
-               if (error) {
-                       set_error(error);
-                       free(error);
-               }
-       }
-
 out_no_removal:
        if (cookie && _dm_use_udev())
                (void)_dm_udev_wait(cookie);
@@ -575,7 +592,7 @@ int dm_query_device(const char *name, uint32_t get_flags,
        struct dm_task *dmt;
        struct dm_info dmi;
        uint64_t start, length, val64;
-       char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3];
+       char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
        const char *tmp_uuid;
        void *next = NULL;
        unsigned int i;
@@ -637,26 +654,60 @@ int dm_query_device(const char *name, uint32_t get_flags,
        if (!params)
                goto out;
        val64 = strtoull(params, &params, 10);
-       if (*params)
-               goto out;
        dmd->offset = val64;
 
-       if (get_flags & DM_ACTIVE_KEY) {
+       /* Features section, available since crypt target version 1.11 */
+       if (*params) {
+               if (*params != ' ')
+                       goto out;
+               params++;
+
+               /* Number of arguments */
+               val64 = strtoull(params, &params, 10);
+               if (*params != ' ')
+                       goto out;
+               params++;
+
+               for (i = 0; i < val64; i++) {
+                       if (!params)
+                               goto out;
+                       arg = strsep(&params, " ");
+                       if (!strcasecmp(arg, "allow_discards"))
+                               dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
+                       else /* unknown option */
+                               goto out;
+               }
+
+               /* All parameters shold be processed */
+               if (params)
+                       goto out;
+       }
+
+       /* Never allow to return empty key */
+       if ((get_flags & DM_ACTIVE_KEY) && dmi.suspended) {
+               log_dbg("Cannot read volume key while suspended.");
+               r = -EINVAL;
+               goto out;
+       }
+
+       if (get_flags & DM_ACTIVE_KEYSIZE) {
                dmd->vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
                if (!dmd->vk) {
                        r = -ENOMEM;
                        goto out;
                }
 
-               buffer[2] = '\0';
-               for(i = 0; i < dmd->vk->keylength; i++) {
-                       memcpy(buffer, &key_[i * 2], 2);
-                       dmd->vk->key[i] = strtoul(buffer, &endp, 16);
-                       if (endp != &buffer[2]) {
-                               crypt_free_volume_key(dmd->vk);
-                               dmd->vk = NULL;
-                               r = -EINVAL;
-                               goto out;
+               if (get_flags & DM_ACTIVE_KEY) {
+                       buffer[2] = '\0';
+                       for(i = 0; i < dmd->vk->keylength; i++) {
+                               memcpy(buffer, &key_[i * 2], 2);
+                               dmd->vk->key[i] = strtoul(buffer, &endp, 16);
+                               if (endp != &buffer[2]) {
+                                       crypt_free_volume_key(dmd->vk);
+                                       dmd->vk = NULL;
+                                       r = -EINVAL;
+                                       goto out;
+                               }
                        }
                }
        }