~psusi/ubuntu/natty/parted/fix-dmraid

« back to all changes in this revision

Viewing changes to libparted/tests/symlink.c

  • Committer: Colin Watson
  • Date: 2010-08-05 21:06:19 UTC
  • mfrom: (7.2.9 sid)
  • Revision ID: cjwatson@canonical.com-20100805210619-f9ld2tuntueagfeo
* Resynchronise with Debian.  Remaining changes:
  - gptsync.dpatch: On Intel Mac systems, write a synced MBR rather than a
    protective MBR.
  - Add -fno-stack-protector on sparc.
  - loop-partitions.dpatch: Loop devices can only have one partition, so
    don't generate device names such as "/dev/loop0p1".
  - udevadm-settle.dpatch: Run 'udevadm settle' either side of rereading
    the partition table, to avoid a variety of races.
  - dmraid.dpatch: Ensure that device-mapper devices for dmraid arrays do
    not have extra nodes created needlessly, as well as making sure that
    partition nodes for dmraid devices are not probed.
  - loop-limits.patch: Remove limits on loop labels.
  - fix-dmraid-regression.path: Reverse upstream change that broke
    installation on dmraid disks for lucid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Sometimes libparted operates on device mapper files, with a path of
 
2
   /dev/mapper/foo. With newer lvm versions /dev/mapper/foo is a symlink to
 
3
   /dev/dm-#. However some storage administration programs (anaconda for
 
4
   example) may do the following:
 
5
   1) Create a ped_device for /dev/mapper/foo
 
6
   2) ped_get_device resolves the symlink to /dev/dm-#, and the path
 
7
      in the PedDevice struct points to /dev/dm-#
 
8
   3) The program does some things to lvm, causing the symlink to
 
9
      point to a different /dev/dm-# node
 
10
   4) The program does something with the PedDevice, which results
 
11
      in an operation on the wrong device
 
12
 
 
13
   Newer libparted versions do not suffer from this problem, as they
 
14
   do not canonicalize device names under /dev/mapper. This test checks
 
15
   for this bug. */
 
16
 
 
17
#include <config.h>
 
18
#include <unistd.h>
 
19
 
 
20
#include <check.h>
 
21
 
 
22
#include <parted/parted.h>
 
23
 
 
24
#include "common.h"
 
25
#include "progname.h"
 
26
 
 
27
static char *temporary_disk;
 
28
 
 
29
static void
 
30
create_disk (void)
 
31
{
 
32
        temporary_disk = _create_disk (4096 * 1024);
 
33
        fail_if (temporary_disk == NULL, "Failed to create temporary disk");
 
34
}
 
35
 
 
36
static void
 
37
destroy_disk (void)
 
38
{
 
39
        unlink (temporary_disk);
 
40
        free (temporary_disk);
 
41
}
 
42
 
 
43
START_TEST (test_symlink)
 
44
{
 
45
        char cwd[256], ln[256] = "/dev/mapper/parted-test-XXXXXX";
 
46
 
 
47
        if (!getcwd (cwd, sizeof cwd)) {
 
48
                fail ("Could not get cwd");
 
49
                return;
 
50
        }
 
51
 
 
52
        /* Create a symlink under /dev/mapper to our
 
53
           temporary disk */
 
54
        int tmp_fd = mkstemp (ln);
 
55
        if (tmp_fd == -1) {
 
56
                fail ("Could not create tempfile");
 
57
                return;
 
58
        }
 
59
 
 
60
        /* There is a temp file vulnerability symlink attack possibility
 
61
           here, but as /dev/mapper is root owned this is a non issue */
 
62
        close (tmp_fd);
 
63
        unlink (ln);
 
64
        char temp_disk_path[256];
 
65
        snprintf (temp_disk_path, sizeof temp_disk_path, "%s/%s", cwd,
 
66
                  temporary_disk);
 
67
        int res = symlink (temp_disk_path, ln);
 
68
        if (res) {
 
69
                fail ("could not create symlink");
 
70
                return;
 
71
        }
 
72
 
 
73
        PedDevice *dev = ped_device_get (ln);
 
74
        if (dev == NULL)
 
75
                goto exit_unlink_ln;
 
76
 
 
77
        /* Create a second temporary_disk */
 
78
        char *temporary_disk2 = _create_disk (4096 * 1024);
 
79
        if (temporary_disk2 == NULL) {
 
80
                fail ("Failed to create 2nd temporary disk");
 
81
                goto exit_destroy_dev;
 
82
        }
 
83
 
 
84
        /* Remove first temporary disk, and make temporary_disk point to
 
85
           temporary_disk2 (for destroy_disk()). */
 
86
        unlink (temporary_disk);
 
87
        free (temporary_disk);
 
88
        temporary_disk = temporary_disk2;
 
89
 
 
90
        /* Update symlink to point to our new / second temporary disk */
 
91
        unlink (ln);
 
92
        snprintf (temp_disk_path, sizeof temp_disk_path, "%s/%s", cwd,
 
93
                  temporary_disk);
 
94
        res = symlink (temp_disk_path, ln);
 
95
        if (res) {
 
96
                fail ("could not create 2nd symlink");
 
97
                goto exit_destroy_dev;
 
98
        }
 
99
 
 
100
        /* Do something to our PedDevice, if the symlink was resolved,
 
101
           instead of remembering the /dev/mapper/foo name, this will fail */
 
102
        ped_disk_clobber (dev);
 
103
 
 
104
exit_destroy_dev:
 
105
        ped_device_destroy (dev);
 
106
exit_unlink_ln:
 
107
        unlink (ln);
 
108
}
 
109
END_TEST
 
110
 
 
111
int
 
112
main (int argc, char **argv)
 
113
{
 
114
        set_program_name (argv[0]);
 
115
        int number_failed;
 
116
        Suite* suite = suite_create ("Symlink");
 
117
        TCase* tcase_symlink = tcase_create ("/dev/mapper symlink");
 
118
 
 
119
        /* Fail when an exception is raised */
 
120
        ped_exception_set_handler (_test_exception_handler);
 
121
 
 
122
        tcase_add_checked_fixture (tcase_symlink, create_disk, destroy_disk);
 
123
        tcase_add_test (tcase_symlink, test_symlink);
 
124
        /* Disable timeout for this test */
 
125
        tcase_set_timeout (tcase_symlink, 0);
 
126
        suite_add_tcase (suite, tcase_symlink);
 
127
 
 
128
        SRunner* srunner = srunner_create (suite);
 
129
        srunner_run_all (srunner, CK_VERBOSE);
 
130
 
 
131
        number_failed = srunner_ntests_failed (srunner);
 
132
        srunner_free (srunner);
 
133
 
 
134
        return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
135
}