~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to tests/kernel/lp-509180/test.c

  • Committer: Tyler Hicks
  • Date: 2017-06-08 23:06:20 UTC
  • mfrom: (885.1.2 ecryptfs)
  • Revision ID: tyhicks@canonical.com-20170608230620-vcosxagt2cwab9eo
ecryptfs-migrate-home: Pass --nopwcheck to ecryptfs-setup-private (LP: #1630477)

[tyhicks: Remove extra spaces in usage output and add changelog entry]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: Colin King <colin.king@canonical.com>
 
3
 *
 
4
 * Copyright (C) 2012 Canonical, Ltd.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
19
 */
 
20
 
 
21
#include <unistd.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <fcntl.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <attr/xattr.h>
 
30
 
 
31
#define TEST_ERROR      (2)
 
32
 
 
33
#define OFFSET          (9)
 
34
 
 
35
#define OPT_INC         (0x0001)
 
36
#define OPT_DEC         (0x0002)
 
37
#define OPT_XATTR       (0x0004)
 
38
 
 
39
void usage(char *name)
 
40
{
 
41
        fprintf(stderr, "Usage: [-i | -d] [-x] file\n");
 
42
}
 
43
 
 
44
static int test_with_metadata_in_header(int fd, int flags)
 
45
{
 
46
        unsigned char buffer[1];
 
47
 
 
48
        if ((lseek(fd, (off_t)OFFSET, SEEK_SET)) < 0) {
 
49
                fprintf(stderr, "Cannot lseek to offset %d: %s\n",
 
50
                        OFFSET, strerror(errno));
 
51
                return TEST_ERROR;
 
52
        }
 
53
 
 
54
        if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer)) {
 
55
                fprintf(stderr, "Failed to read\n");
 
56
                return TEST_ERROR;
 
57
        }
 
58
 
 
59
        if (flags & OPT_INC)
 
60
                buffer[0]++;
 
61
 
 
62
        if (flags & OPT_DEC)
 
63
                buffer[0]--;
 
64
 
 
65
        if ((lseek(fd, (off_t)OFFSET, SEEK_SET)) < 0) {
 
66
                fprintf(stderr, "Cannot lseek to offset %d: %s\n",
 
67
                        OFFSET, strerror(errno));
 
68
                return TEST_ERROR;
 
69
        }
 
70
 
 
71
        if (write(fd, buffer, sizeof(buffer)) != sizeof(buffer)) {
 
72
                fprintf(stderr, "Failed to write\n");
 
73
                return TEST_ERROR;
 
74
        }
 
75
 
 
76
        return 0;
 
77
}
 
78
 
 
79
static int test_with_metadata_in_xattr(int fd, int flags)
 
80
{
 
81
        const char *name = "user.ecryptfs";
 
82
        unsigned char *value = NULL;
 
83
        ssize_t nread, size = 0;
 
84
        int rc = TEST_ERROR;
 
85
 
 
86
        size = fgetxattr(fd, name, value, size);
 
87
        if (size < 0) {
 
88
                fprintf(stderr, "Cannot retrieve xattr size: %s\n",
 
89
                        strerror(errno));
 
90
                goto out;
 
91
        }
 
92
 
 
93
        value = malloc(size);
 
94
        if (!value) {
 
95
                fprintf(stderr,
 
96
                        "Cannot allocate memory to store the xattr value\n");
 
97
                goto out;
 
98
        }
 
99
 
 
100
        nread = fgetxattr(fd, name, value, size);
 
101
        if (nread != size) {
 
102
                if (nread < 0)
 
103
                        fprintf(stderr, "Cannot read xattr: %s\n",
 
104
                                strerror(errno));
 
105
                else
 
106
                        fprintf(stderr, "Partial xattr read: %zu < %zu\n",
 
107
                                nread, size);
 
108
                goto out;
 
109
        }
 
110
 
 
111
        if (flags & OPT_INC)
 
112
                value[OFFSET]++;
 
113
 
 
114
        if (flags & OPT_DEC)
 
115
                value[OFFSET]--;
 
116
 
 
117
        if (fsetxattr(fd, name, value, size, XATTR_REPLACE) < 0) {
 
118
                fprintf(stderr, "Cannot write xattr: %s\n", strerror(errno));
 
119
                goto out;
 
120
        }
 
121
 
 
122
        rc = 0;
 
123
out:
 
124
        free(value);
 
125
 
 
126
        return rc;
 
127
}
 
128
 
 
129
 
 
130
/*
 
131
 *  https://bugs.launchpad.net/ecryptfs/+bug/509180
 
132
 *  Increment/Decrement 9th byte in lower file
 
133
 */
 
134
int main(int argc, char **argv)
 
135
{
 
136
        int fd;
 
137
        int opt, flags = 0;
 
138
        int rc = 0;
 
139
        char *file;
 
140
 
 
141
        if (argc < 3) {
 
142
                usage(argv[0]);
 
143
                exit(TEST_ERROR);
 
144
        }
 
145
 
 
146
        while ((opt = getopt(argc, argv, "idx")) != -1) {
 
147
                switch (opt) {
 
148
                case 'i':
 
149
                        flags |= OPT_INC;
 
150
                        break;
 
151
                case 'd':
 
152
                        flags |= OPT_DEC;
 
153
                        break;
 
154
                case 'x':
 
155
                        flags |= OPT_XATTR;
 
156
                        break;
 
157
                default:
 
158
                        usage(argv[0]);
 
159
                        exit(TEST_ERROR);
 
160
                }
 
161
        }
 
162
 
 
163
        if ((flags == 0) || (flags == (OPT_INC | OPT_DEC))) {
 
164
                fprintf(stderr, "Need to specify -i or -d\n");
 
165
                exit(TEST_ERROR);
 
166
        }
 
167
 
 
168
        file = argv[optind];
 
169
 
 
170
        if ((fd = open(file, O_RDWR, 0700)) < 0) {
 
171
                fprintf(stderr, "Cannot open %s : %s\n", file, strerror(errno));
 
172
                exit(TEST_ERROR);
 
173
        }
 
174
 
 
175
        if (flags & OPT_XATTR)
 
176
                rc = test_with_metadata_in_xattr(fd, flags);
 
177
        else
 
178
                rc = test_with_metadata_in_header(fd, flags);
 
179
 
 
180
tidy:
 
181
        if (close(fd) < 0) {
 
182
                fprintf(stderr, "Close failed: %s\n", strerror(errno));
 
183
                exit(TEST_ERROR);
 
184
        }
 
185
 
 
186
        exit(rc);
 
187
}