Revert "Imported upstream version 1.6.7"
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
index c5826e5..c2b8405 100644 (file)
@@ -3,17 +3,18 @@
  *
  * Copyright (C) 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
- * version 2 as published by the Free Software Foundation.
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This file is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
@@ -52,20 +53,22 @@ struct verity_sb {
 
 /* Read verity superblock from disk */
 int VERITY_read_sb(struct crypt_device *cd,
-                  const char *device,
                   uint64_t sb_offset,
                   char **uuid_string,
                   struct crypt_params_verity *params)
 {
+       struct device *device = crypt_metadata_device(cd);
+       int bsize = device_block_size(device);
        struct verity_sb sb = {};
        ssize_t hdr_size = sizeof(struct verity_sb);
        int devfd = 0, sb_version;
 
        log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
-               sizeof(struct verity_sb), device, sb_offset);
+               sizeof(struct verity_sb), device_path(device), sb_offset);
 
        if (params->flags & CRYPT_VERITY_NO_HEADER) {
-               log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
+               log_err(cd, _("Verity device doesn't use on-disk header.\n"),
+                       device_path(device));
                return -EINVAL;
        }
 
@@ -74,21 +77,22 @@ int VERITY_read_sb(struct crypt_device *cd,
                return -EINVAL;
        }
 
-       devfd = open(device ,O_RDONLY | O_DIRECT);
+       devfd = device_open(device, O_RDONLY);
        if(devfd == -1) {
-               log_err(cd, _("Cannot open device %s.\n"), device);
+               log_err(cd, _("Cannot open device %s.\n"), device_path(device));
                return -EINVAL;
        }
 
        if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
-          read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
+          read_blockwise(devfd, bsize, &sb, hdr_size) < hdr_size) {
                close(devfd);
                return -EIO;
        }
        close(devfd);
 
        if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
-               log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
+               log_err(cd, _("Device %s is not a valid VERITY device.\n"),
+                       device_path(device));
                return -EINVAL;
        }
 
@@ -144,32 +148,35 @@ int VERITY_read_sb(struct crypt_device *cd,
 
 /* Write verity superblock to disk */
 int VERITY_write_sb(struct crypt_device *cd,
-                  const char *device,
                   uint64_t sb_offset,
                   const char *uuid_string,
                   struct crypt_params_verity *params)
 {
+       struct device *device = crypt_metadata_device(cd);
+       int bsize = device_block_size(device);
        struct verity_sb sb = {};
        ssize_t hdr_size = sizeof(struct verity_sb);
        uuid_t uuid;
        int r, devfd = 0;
 
        log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
-               sizeof(struct verity_sb), device, sb_offset);
+               sizeof(struct verity_sb), device_path(device), sb_offset);
 
        if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
-               log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
+               log_err(cd, _("Wrong VERITY UUID format provided.\n"),
+                       device_path(device));
                return -EINVAL;
        }
 
        if (params->flags & CRYPT_VERITY_NO_HEADER) {
-               log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
+               log_err(cd, _("Verity device doesn't use on-disk header.\n"),
+                       device_path(device));
                return -EINVAL;
        }
 
-       devfd = open(device, O_RDWR | O_DIRECT);
+       devfd = device_open(device, O_RDWR);
        if(devfd == -1) {
-               log_err(cd, _("Cannot open device %s.\n"), device);
+               log_err(cd, _("Cannot open device %s.\n"), device_path(device));
                return -EINVAL;
        }
 
@@ -184,9 +191,10 @@ int VERITY_write_sb(struct crypt_device *cd,
        memcpy(sb.salt, params->salt, params->salt_size);
        memcpy(sb.uuid, uuid, sizeof(sb.uuid));
 
-       r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
+       r = write_lseek_blockwise(devfd, bsize, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
        if (r)
-               log_err(cd, _("Error during update of verity header on device %s.\n"), device);
+               log_err(cd, _("Error during update of verity header on device %s.\n"),
+                       device_path(device));
        close(devfd);
 
        return r;
@@ -220,22 +228,20 @@ int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
 /* Activate verity device in kernel device-mapper */
 int VERITY_activate(struct crypt_device *cd,
                     const char *name,
-                    const char *hash_device,
                     const char *root_hash,
                     size_t root_hash_size,
                     struct crypt_params_verity *verity_hdr,
                     uint32_t activation_flags)
 {
        struct crypt_dm_active_device dmd;
-       uint64_t offset = 0;
        int r;
 
        log_dbg("Trying to activate VERITY device %s using hash %s.",
                name ?: "[none]", verity_hdr->hash_name);
 
        if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
+               log_dbg("Verification of data in userspace required.");
                r = VERITY_verify(cd, verity_hdr,
-                                 crypt_get_device_name(cd), hash_device,
                                  root_hash, root_hash_size);
                if (r < 0)
                        return r;
@@ -245,8 +251,8 @@ int VERITY_activate(struct crypt_device *cd,
                return 0;
 
        dmd.target = DM_VERITY;
-       dmd.data_device = crypt_get_device_name(cd);
-       dmd.u.verity.hash_device = hash_device;
+       dmd.data_device = crypt_data_device(cd);
+       dmd.u.verity.hash_device = crypt_metadata_device(cd);
        dmd.u.verity.root_hash = root_hash;
        dmd.u.verity.root_hash_size = root_hash_size;
        dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
@@ -255,20 +261,25 @@ int VERITY_activate(struct crypt_device *cd,
        dmd.uuid = crypt_get_uuid(cd);
        dmd.u.verity.vp = verity_hdr;
 
-       r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
-                                   &dmd.size, &offset, &dmd.flags);
+       r = device_block_adjust(cd, dmd.u.verity.hash_device, DEV_OK,
+                               0, NULL, NULL);
        if (r)
                return r;
 
-       r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
-       if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
+       r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
+                               0, &dmd.size, &dmd.flags);
+       if (r)
+               return r;
+
+       r = dm_create_device(cd, name, CRYPT_VERITY, &dmd, 0);
+       if (r < 0 && !(dm_flags() & DM_VERITY_SUPPORTED)) {
                log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
                return -ENOTSUP;
        }
        if (r < 0)
                return r;
 
-       r = dm_status_verity_ok(name);
+       r = dm_status_verity_ok(cd, name);
        if (r < 0)
                return r;