1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include "bit-rot-object-version.h"
/* NOTE: no size discovery */
int
brstub_validate_version (char *bpath, unsigned long version)
{
int ret = 0;
int match = 0;
size_t xsize = 0;
br_version_t *xv = NULL;
xsize = sizeof (br_version_t);
xv = calloc (1, xsize);
if (!xv)
goto err;
ret = getxattr (bpath, "trusted.bit-rot.version", xv, xsize);
if (ret < 0)
goto err;
if (xv->ongoingversion != version)
match = -1;
free (xv);
return match;
err:
return -1;
}
int
brstub_open_validation (char *filp, char *bpath, unsigned long startversion)
{
int fd1 = 0;
int fd2 = 0;
int ret = 0;
/* read only check */
fd1 = open (filp, O_RDONLY);
if (fd1 < 0)
goto err;
close (fd1);
ret = brstub_validate_version (bpath, startversion);
if (ret < 0)
goto err;
/* single open (write/) check */
fd1 = open (filp, O_RDWR);
if (fd1 < 0)
goto err;
close (fd1);
startversion++;
ret = brstub_validate_version (bpath, startversion);
/* multi open (write/) check */
fd1 = open (filp, O_RDWR);
if (fd1 < 0)
goto err;
fd2 = open (filp, O_WRONLY);
if (fd1 < 0)
goto err;
close (fd1);
close (fd2);
/**
* incremented once per open()/open().../close()/close() sequence
*/
startversion++;
ret = brstub_validate_version (bpath, startversion);
if (ret < 0)
goto err;
return 0;
err:
return -1;
}
int
brstub_new_object_validate (char *filp, char *brick)
{
int ret = 0;
char *fname = NULL;
char bpath[PATH_MAX] = {0,};
fname = basename (filp);
if (!fname)
goto err;
(void) snprintf (bpath, PATH_MAX, "%s/%s", brick, fname);
printf ("Validating initial version..\n");
ret = brstub_validate_version (bpath, 1);
if (ret < 0)
goto err;
printf ("Validating version on modifications..\n");
ret = brstub_open_validation (filp, bpath, 1);
if (ret < 0)
goto err;
return 0;
err:
return -1;
}
int
main (int argc, char **argv)
{
int ret = 0;
char *filp = NULL;
char *brick = NULL;
if (argc != 3) {
printf ("Usage: %s <path> <brick>\n", argv[0]);
goto err;
}
filp = argv[1];
brick = argv[2];
printf ("Validating object version [%s]\n", filp);
ret = brstub_new_object_validate (filp, brick);
if (ret < 0)
goto err;
return 0;
err:
return -1;
}
|