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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
--- sys/fs/nfsclient/nfs_clrpcops.c.orig
+++ sys/fs/nfsclient/nfs_clrpcops.c
@@ -142,6 +142,7 @@
nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *,
NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *,
int *, int *);
+static bool nfscl_invalidfname(bool, char *, int);
static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *,
struct nfscllockowner *, u_int64_t, u_int64_t,
u_int32_t, struct ucred *, NFSPROC_T *, int);
@@ -3279,6 +3280,31 @@
return (error);
}
+/*
+ * Check to make sure the file name in a Readdir reply is valid.
+ */
+static bool
+nfscl_invalidfname(bool is_v4, char *name, int len)
+{
+ int i;
+ char *cp;
+
+ if (is_v4 && ((len == 1 && name[0] == '.') ||
+ (len == 2 && name[0] == '.' && name[1] == '.'))) {
+ printf("Readdir NFSv4 reply has dot or dotdot in it\n");
+ return (true);
+ }
+ cp = name;
+ for (i = 0; i < len; i++, cp++) {
+ if (*cp == '/' || *cp == '\0') {
+ printf("Readdir reply file name had imbedded / or nul"
+ " byte\n");
+ return (true);
+ }
+ }
+ return (false);
+}
+
/*
* Readdir rpc.
* Always returns with either uio_resid unchanged, if you are at the
@@ -3331,6 +3357,8 @@
KASSERT(uiop->uio_iovcnt == 1 &&
(uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
("nfs readdirrpc bad uio"));
+ KASSERT(uiop->uio_segflg == UIO_SYSSPACE,
+ ("nfsrpc_readdir: uio userspace"));
ncookie.lval[0] = ncookie.lval[1] = 0;
/*
* There is no point in reading a lot more than uio_resid, however
@@ -3588,6 +3616,17 @@
uiop->uio_resid)
bigenough = 0;
if (bigenough) {
+ struct iovec saviov;
+ off_t savoff;
+ ssize_t savresid;
+ int savblksiz;
+
+ saviov.iov_base = uiop->uio_iov->iov_base;
+ saviov.iov_len = uiop->uio_iov->iov_len;
+ savoff = uiop->uio_offset;
+ savresid = uiop->uio_resid;
+ savblksiz = blksiz;
+
dp = (struct dirent *)uiop->uio_iov->iov_base;
dp->d_pad0 = dp->d_pad1 = 0;
dp->d_off = 0;
@@ -3603,20 +3642,35 @@
uiop->uio_iov->iov_base =
(char *)uiop->uio_iov->iov_base + DIRHDSIZ;
uiop->uio_iov->iov_len -= DIRHDSIZ;
+ cp = uiop->uio_iov->iov_base;
error = nfsm_mbufuio(nd, uiop, len);
if (error)
goto nfsmout;
- cp = uiop->uio_iov->iov_base;
- tlen -= len;
- NFSBZERO(cp, tlen);
- cp += tlen; /* points to cookie storage */
- tl2 = (u_int32_t *)cp;
- uiop->uio_iov->iov_base =
- (char *)uiop->uio_iov->iov_base + tlen +
- NFSX_HYPER;
- uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
- uiop->uio_resid -= tlen + NFSX_HYPER;
- uiop->uio_offset += (tlen + NFSX_HYPER);
+ /* Check for an invalid file name. */
+ if (nfscl_invalidfname(
+ (nd->nd_flag & ND_NFSV4) != 0, cp, len)) {
+ /* Skip over this entry. */
+ uiop->uio_iov->iov_base =
+ saviov.iov_base;
+ uiop->uio_iov->iov_len =
+ saviov.iov_len;
+ uiop->uio_offset = savoff;
+ uiop->uio_resid = savresid;
+ blksiz = savblksiz;
+ } else {
+ cp = uiop->uio_iov->iov_base;
+ tlen -= len;
+ NFSBZERO(cp, tlen);
+ cp += tlen; /* points to cookie store */
+ tl2 = (u_int32_t *)cp;
+ uiop->uio_iov->iov_base =
+ (char *)uiop->uio_iov->iov_base +
+ tlen + NFSX_HYPER;
+ uiop->uio_iov->iov_len -= tlen +
+ NFSX_HYPER;
+ uiop->uio_resid -= tlen + NFSX_HYPER;
+ uiop->uio_offset += (tlen + NFSX_HYPER);
+ }
} else {
error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
if (error)
@@ -3782,6 +3836,8 @@
KASSERT(uiop->uio_iovcnt == 1 &&
(uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
("nfs readdirplusrpc bad uio"));
+ KASSERT(uiop->uio_segflg == UIO_SYSSPACE,
+ ("nfsrpc_readdirplus: uio userspace"));
ncookie.lval[0] = ncookie.lval[1] = 0;
timespecclear(&dctime);
*attrflagp = 0;
@@ -4017,6 +4073,17 @@
uiop->uio_resid)
bigenough = 0;
if (bigenough) {
+ struct iovec saviov;
+ off_t savoff;
+ ssize_t savresid;
+ int savblksiz;
+
+ saviov.iov_base = uiop->uio_iov->iov_base;
+ saviov.iov_len = uiop->uio_iov->iov_len;
+ savoff = uiop->uio_offset;
+ savresid = uiop->uio_resid;
+ savblksiz = blksiz;
+
dp = (struct dirent *)uiop->uio_iov->iov_base;
dp->d_pad0 = dp->d_pad1 = 0;
dp->d_off = 0;
@@ -4035,25 +4102,41 @@
cnp->cn_nameptr = uiop->uio_iov->iov_base;
cnp->cn_namelen = len;
NFSCNHASHZERO(cnp);
+ cp = uiop->uio_iov->iov_base;
error = nfsm_mbufuio(nd, uiop, len);
if (error)
goto nfsmout;
- cp = uiop->uio_iov->iov_base;
- tlen -= len;
- NFSBZERO(cp, tlen);
- cp += tlen; /* points to cookie storage */
- tl2 = (u_int32_t *)cp;
- if (len == 2 && cnp->cn_nameptr[0] == '.' &&
- cnp->cn_nameptr[1] == '.')
- isdotdot = 1;
- else
- isdotdot = 0;
- uiop->uio_iov->iov_base =
- (char *)uiop->uio_iov->iov_base + tlen +
- NFSX_HYPER;
- uiop->uio_iov->iov_len -= tlen + NFSX_HYPER;
- uiop->uio_resid -= tlen + NFSX_HYPER;
- uiop->uio_offset += (tlen + NFSX_HYPER);
+ /* Check for an invalid file name. */
+ if (nfscl_invalidfname(
+ (nd->nd_flag & ND_NFSV4) != 0, cp, len)) {
+ /* Skip over this entry. */
+ uiop->uio_iov->iov_base =
+ saviov.iov_base;
+ uiop->uio_iov->iov_len =
+ saviov.iov_len;
+ uiop->uio_offset = savoff;
+ uiop->uio_resid = savresid;
+ blksiz = savblksiz;
+ } else {
+ cp = uiop->uio_iov->iov_base;
+ tlen -= len;
+ NFSBZERO(cp, tlen);
+ cp += tlen; /* points to cookie store */
+ tl2 = (u_int32_t *)cp;
+ if (len == 2 &&
+ cnp->cn_nameptr[0] == '.' &&
+ cnp->cn_nameptr[1] == '.')
+ isdotdot = 1;
+ else
+ isdotdot = 0;
+ uiop->uio_iov->iov_base =
+ (char *)uiop->uio_iov->iov_base +
+ tlen + NFSX_HYPER;
+ uiop->uio_iov->iov_len -= tlen +
+ NFSX_HYPER;
+ uiop->uio_resid -= tlen + NFSX_HYPER;
+ uiop->uio_offset += (tlen + NFSX_HYPER);
+ }
} else {
error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
if (error)
|