]> wimlib.net Git - wimlib/blobdiff - src/test_support.c
wlfuzz: compare timestamps
[wimlib] / src / test_support.c
index a7a92f52ef790e07b746a7908eb32475b04b5c37..4df34d4cc8a8f06278313ddac92da82333121ecf 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2015-2016 Eric Biggers
+ * Copyright (C) 2015-2017 Eric Biggers
  *
  * 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
@@ -35,6 +35,9 @@
 
 #include <ctype.h>
 #include <math.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "wimlib.h"
 #include "wimlib/endianness.h"
@@ -47,6 +50,8 @@
 #include "wimlib/scan.h"
 #include "wimlib/security_descriptor.h"
 #include "wimlib/test_support.h"
+#include "wimlib/unix_data.h"
+#include "wimlib/xattr.h"
 
 /*----------------------------------------------------------------------------*
  *                            File tree generation                            *
@@ -377,6 +382,162 @@ generate_random_security_descriptor(void *_desc, struct generation_context *ctx)
        return p - (u8 *)desc;
 }
 
+static bool
+am_root(void)
+{
+#ifdef __WIN32__
+       return false;
+#else
+       return (getuid() == 0);
+#endif
+}
+
+static u32
+generate_uid(void)
+{
+#ifdef __WIN32__
+       return 0;
+#else
+       if (am_root())
+               return rand32();
+       return getuid();
+#endif
+}
+
+static u32
+generate_gid(void)
+{
+#ifdef __WIN32__
+       return 0;
+#else
+       if (am_root())
+               return rand32();
+       return getgid();
+#endif
+}
+
+#ifdef __WIN32__
+#  ifndef S_IFLNK
+#    define S_IFLNK  0120000
+#  endif
+#  ifndef S_IFSOCK
+#    define S_IFSOCK 0140000
+#  endif
+#endif
+
+static int
+set_random_unix_metadata(struct wim_inode *inode)
+{
+       struct wimlib_unix_data dat;
+
+       dat.uid = generate_uid();
+       dat.gid = generate_gid();
+       if (inode_is_symlink(inode))
+               dat.mode = S_IFLNK | 0777;
+       else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
+               dat.mode = S_IFDIR | 0700 | (rand32() % 07777);
+       else if (is_zero_hash(inode_get_hash_of_unnamed_data_stream(inode)) &&
+                randbool() && am_root())
+       {
+               dat.mode = rand32() % 07777;
+               switch (rand32() % 4) {
+               case 0:
+                       dat.mode |= S_IFIFO;
+                       break;
+               case 1:
+                       dat.mode |= S_IFCHR;
+                       dat.rdev = 261; /* /dev/zero */
+                       break;
+               case 2:
+                       dat.mode |= S_IFBLK;
+                       dat.rdev = 261; /* /dev/zero */
+                       break;
+               default:
+                       dat.mode |= S_IFSOCK;
+                       break;
+               }
+       } else {
+               dat.mode = S_IFREG | 0400 | (rand32() % 07777);
+       }
+       dat.rdev = 0;
+
+       if (!inode_set_unix_data(inode, &dat, UNIX_DATA_ALL))
+               return WIMLIB_ERR_NOMEM;
+
+       return 0;
+}
+
+static noinline_for_stack int
+set_random_xattrs(struct wim_inode *inode)
+{
+       int num_xattrs = 1 + rand32() % 16;
+       char entries[8192] _aligned_attribute(4);
+       struct wimlib_xattr_entry *entry = (void *)entries;
+       size_t entries_size;
+       struct wimlib_unix_data unix_data;
+       const char *prefix = "user.";
+       static const char capability_name[] = "security.capability";
+       bool generated_capability_xattr = false;
+
+       /*
+        * On Linux, xattrs in the "user" namespace are only permitted on
+        * regular files and directories.  For other types of files we can use
+        * the "trusted" namespace, but this requires root.
+        */
+       if (inode_is_symlink(inode) ||
+           (inode_get_unix_data(inode, &unix_data) &&
+            !S_ISREG(unix_data.mode) && !S_ISDIR(unix_data.mode)))
+       {
+               if (!am_root())
+                       return 0;
+               prefix = "trusted.";
+       }
+
+       for (int i = 0; i < num_xattrs; i++) {
+               int value_len = rand32() % 64;
+               u8 *p;
+
+               entry->reserved = 0;
+               entry->value_len = cpu_to_le32(value_len);
+
+               if (rand32() % 16 == 0 && am_root() &&
+                   !generated_capability_xattr) {
+                       int name_len = sizeof(capability_name) - 1;
+                       entry->name_len = cpu_to_le16(name_len);
+                       p = mempcpy(entry->name, capability_name, name_len);
+                       generated_capability_xattr = true;
+               } else {
+                       int name_len = 1 + rand32() % 64;
+
+                       entry->name_len = cpu_to_le16(strlen(prefix) + name_len);
+                       p = mempcpy(entry->name, prefix, strlen(prefix));
+                       *p++ = 'a' + i;
+                       for (int j = 1; j < name_len; j++) {
+                               do {
+                                       *p = rand8();
+                               } while (*p == '\0');
+                               p++;
+                       }
+               }
+               for (int j = 0; j < value_len; j++)
+                       *p++ = rand8();
+
+               while ((uintptr_t)p % 4)
+                       *p++ = 0;
+
+               entry = (void *)p;
+       }
+
+       entries_size = (char *)entry - entries;
+       wimlib_assert(entries_size > 0 && entries_size % 4 == 0 &&
+                     entries_size <= sizeof(entries));
+
+       if (!inode_set_linux_xattrs(inode, entries, entries_size))
+               return WIMLIB_ERR_NOMEM;
+
+       return 0;
+}
+
 static int
 set_random_metadata(struct wim_inode *inode, struct generation_context *ctx)
 {
@@ -421,6 +582,20 @@ set_random_metadata(struct wim_inode *inode, struct generation_context *ctx)
                        return WIMLIB_ERR_NOMEM;
        }
 
+       /* Standard UNIX permissions and special files */
+       if (rand32() % 16 == 0) {
+               int ret = set_random_unix_metadata(inode);
+               if (ret)
+                       return ret;
+       }
+
+       /* Extended attributes */
+       if (rand32() % 32 == 0) {
+               int ret = set_random_xattrs(inode);
+               if (ret)
+                       return ret;
+       }
+
        return 0;
 
 }
@@ -1172,6 +1347,203 @@ cmp_object_ids(const struct wim_inode *inode1,
        return 0;
 }
 
+static int
+cmp_unix_metadata(const struct wim_inode *inode1,
+                 const struct wim_inode *inode2, int cmp_flags)
+{
+       struct wimlib_unix_data dat1, dat2;
+       bool present1, present2;
+
+       present1 = inode_get_unix_data(inode1, &dat1);
+       present2 = inode_get_unix_data(inode2, &dat2);
+
+       if (!present1 && !present2)
+               return 0;
+
+       if (present1 && !present2) {
+               if (cmp_flags & (WIMLIB_CMP_FLAG_NTFS_3G_MODE |
+                                WIMLIB_CMP_FLAG_WINDOWS_MODE))
+                       return 0;
+               ERROR("%"TS" unexpectedly lost its UNIX metadata",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       }
+
+       if (!present1 && present2) {
+               if (cmp_flags & WIMLIB_CMP_FLAG_UNIX_MODE)
+                       return 0;
+               ERROR("%"TS" unexpectedly gained UNIX metadata",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       }
+
+       if (memcmp(&dat1, &dat2, sizeof(dat1)) != 0) {
+               ERROR("UNIX metadata of %"TS" differs: "
+                     "[uid=%u, gid=%u, mode=0%o, rdev=%u] vs. "
+                     "[uid=%u, gid=%u, mode=0%o, rdev=%u]",
+                     inode_any_full_path(inode1),
+                     dat1.uid, dat1.gid, dat1.mode, dat1.rdev,
+                     dat2.uid, dat2.gid, dat2.mode, dat2.rdev);
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       }
+
+       return 0;
+}
+
+static int
+cmp_xattr_names(const void *p1, const void *p2)
+{
+       const struct wimlib_xattr_entry *entry1 = *(const struct wimlib_xattr_entry **)p1;
+       const struct wimlib_xattr_entry *entry2 = *(const struct wimlib_xattr_entry **)p2;
+       u16 name_len1 = le16_to_cpu(entry1->name_len);
+       u16 name_len2 = le16_to_cpu(entry2->name_len);
+       int res;
+
+       res = cmp_u32(name_len1, name_len2);
+       if (res)
+               return res;
+
+       return memcmp(entry1->name, entry2->name, name_len1);
+}
+
+/* Validate and sort by name a list of extended attributes */
+static int
+parse_xattrs(const void *xattrs, u32 len,
+            const struct wimlib_xattr_entry *entries[],
+            u32 *num_entries_p)
+{
+       u32 limit = *num_entries_p;
+       u32 num_entries = 0;
+       const struct wimlib_xattr_entry *entry = xattrs;
+
+       while ((void *)entry < xattrs + len) {
+               if (!valid_xattr_entry(entry, xattrs + len - (void *)entry)) {
+                       ERROR("Invalid xattr entry");
+                       return WIMLIB_ERR_INVALID_XATTR;
+               }
+               if (num_entries >= limit) {
+                       ERROR("Too many xattr entries");
+                       return WIMLIB_ERR_INVALID_XATTR;
+               }
+               entries[num_entries++] = entry;
+               entry = xattr_entry_next(entry);
+       }
+
+       if (num_entries == 0) {
+               ERROR("No xattr entries");
+               return WIMLIB_ERR_INVALID_XATTR;
+       }
+
+       qsort(entries, num_entries, sizeof(entries[0]), cmp_xattr_names);
+
+       for (u32 i = 1; i < num_entries; i++) {
+               if (cmp_xattr_names(&entries[i - 1], &entries[i]) == 0) {
+                       ERROR("Duplicate xattr names");
+                       return WIMLIB_ERR_INVALID_XATTR;
+               }
+       }
+
+       *num_entries_p = num_entries;
+       return 0;
+}
+
+static int
+cmp_linux_xattrs(const struct wim_inode *inode1,
+                const struct wim_inode *inode2, int cmp_flags)
+{
+       const void *xattrs1, *xattrs2;
+       u32 len1, len2;
+
+       xattrs1 = inode_get_linux_xattrs(inode1, &len1);
+       xattrs2 = inode_get_linux_xattrs(inode2, &len2);
+
+       if (!xattrs1 && !xattrs2) {
+               return 0;
+       } else if (xattrs1 && !xattrs2) {
+               if (cmp_flags & (WIMLIB_CMP_FLAG_NTFS_3G_MODE |
+                                WIMLIB_CMP_FLAG_WINDOWS_MODE))
+                       return 0;
+               ERROR("%"TS" unexpectedly lost its xattrs",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       } else if (!xattrs1 && xattrs2) {
+               ERROR("%"TS" unexpectedly gained xattrs",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       } else {
+               const int max_entries = 64;
+               const struct wimlib_xattr_entry *entries1[max_entries];
+               const struct wimlib_xattr_entry *entries2[max_entries];
+               u32 xattr_count1 = max_entries;
+               u32 xattr_count2 = max_entries;
+               int ret;
+
+               ret = parse_xattrs(xattrs1, len1, entries1, &xattr_count1);
+               if (ret) {
+                       ERROR("%"TS": invalid xattrs",
+                             inode_any_full_path(inode1));
+                       return ret;
+               }
+               ret = parse_xattrs(xattrs2, len2, entries2, &xattr_count2);
+               if (ret) {
+                       ERROR("%"TS": invalid xattrs",
+                             inode_any_full_path(inode2));
+                       return ret;
+               }
+               if (xattr_count1 != xattr_count2) {
+                       ERROR("%"TS": number of xattrs changed.  had %u "
+                             "before, now has %u", inode_any_full_path(inode1),
+                             xattr_count1, xattr_count2);
+               }
+               for (u32 i = 0; i < xattr_count1; i++) {
+                       const struct wimlib_xattr_entry *entry1 = entries1[i];
+                       const struct wimlib_xattr_entry *entry2 = entries2[i];
+
+                       if (entry1->name_len != entry2->name_len ||
+                           entry1->value_len != entry2->value_len ||
+                           entry1->reserved != entry2->reserved ||
+                           memcmp(entry1->name, entry2->name,
+                                  le16_to_cpu(entry1->name_len)) ||
+                           memcmp(entry1->name + le16_to_cpu(entry1->name_len),
+                                  entry2->name + le16_to_cpu(entry1->name_len),
+                                  le32_to_cpu(entry1->value_len)))
+                       {
+                               ERROR("xattr %.*s of %"TS" differs",
+                                     le16_to_cpu(entry1->name_len),
+                                     entry1->name, inode_any_full_path(inode1));
+                               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+                       }
+               }
+               return 0;
+       }
+}
+
+static int
+cmp_timestamps(const struct wim_inode *inode1, const struct wim_inode *inode2,
+              int cmp_flags)
+{
+       if (inode1->i_creation_time != inode2->i_creation_time &&
+           !(cmp_flags & WIMLIB_CMP_FLAG_UNIX_MODE)) {
+               ERROR("Creation time of %"TS" differs",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       }
+
+       if (inode1->i_last_write_time != inode2->i_last_write_time) {
+               ERROR("Last write time of %"TS" differs",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       }
+
+       if (inode1->i_last_access_time != inode2->i_last_access_time) {
+               ERROR("Last access time of %"TS" differs",
+                     inode_any_full_path(inode1));
+               return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+       }
+
+       return 0;
+}
+
 static int
 cmp_inodes(const struct wim_inode *inode1, const struct wim_inode *inode2,
           const struct wim_image_metadata *imd1,
@@ -1252,6 +1624,21 @@ cmp_inodes(const struct wim_inode *inode1, const struct wim_inode *inode2,
        if (ret)
                return ret;
 
+       /* Compare timestamps  */
+       ret = cmp_timestamps(inode1, inode2, cmp_flags);
+       if (ret)
+               return ret;
+
+       /* Compare standard UNIX metadata  */
+       ret = cmp_unix_metadata(inode1, inode2, cmp_flags);
+       if (ret)
+               return ret;
+
+       /* Compare Linux-style xattrs  */
+       ret = cmp_linux_xattrs(inode1, inode2, cmp_flags);
+       if (ret)
+               return ret;
+
        return 0;
 }