Skip to content

Commit 815d3c1

Browse files
committed
Merge tag 'pull-ceph-d_name-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull ceph dentry->d_name fixes from Al Viro: "Stuff that had fallen through the cracks back in February; ceph folks tested that pile and said they prefer to have it go through my tree..." * tag 'pull-ceph-d_name-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: ceph: fix a race with rename() in ceph_mdsc_build_path() prep for ceph_encode_encrypted_fname() fixes [ceph] parse_longname(): strrchr() expects NUL-terminated string
2 parents 2d9c133 + 0d2da25 commit 815d3c1

File tree

5 files changed

+43
-86
lines changed

5 files changed

+43
-86
lines changed

fs/ceph/caps.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4957,24 +4957,20 @@ int ceph_encode_dentry_release(void **p, struct dentry *dentry,
49574957
cl = ceph_inode_to_client(dir);
49584958
spin_lock(&dentry->d_lock);
49594959
if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4960+
int len = dentry->d_name.len;
49604961
doutc(cl, "%p mds%d seq %d\n", dentry, mds,
49614962
(int)di->lease_seq);
49624963
rel->dname_seq = cpu_to_le32(di->lease_seq);
49634964
__ceph_mdsc_drop_dentry_lease(dentry);
4965+
memcpy(*p, dentry->d_name.name, len);
49644966
spin_unlock(&dentry->d_lock);
49654967
if (IS_ENCRYPTED(dir) && fscrypt_has_encryption_key(dir)) {
4966-
int ret2 = ceph_encode_encrypted_fname(dir, dentry, *p);
4967-
4968-
if (ret2 < 0)
4969-
return ret2;
4970-
4971-
rel->dname_len = cpu_to_le32(ret2);
4972-
*p += ret2;
4973-
} else {
4974-
rel->dname_len = cpu_to_le32(dentry->d_name.len);
4975-
memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4976-
*p += dentry->d_name.len;
4968+
len = ceph_encode_encrypted_dname(dir, *p, len);
4969+
if (len < 0)
4970+
return len;
49774971
}
4972+
rel->dname_len = cpu_to_le32(len);
4973+
*p += len;
49784974
} else {
49794975
spin_unlock(&dentry->d_lock);
49804976
}

fs/ceph/crypto.c

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -215,35 +215,31 @@ static struct inode *parse_longname(const struct inode *parent,
215215
struct ceph_client *cl = ceph_inode_to_client(parent);
216216
struct inode *dir = NULL;
217217
struct ceph_vino vino = { .snap = CEPH_NOSNAP };
218-
char *inode_number;
219-
char *name_end;
220-
int orig_len = *name_len;
218+
char *name_end, *inode_number;
221219
int ret = -EIO;
222-
220+
/* NUL-terminate */
221+
char *str __free(kfree) = kmemdup_nul(name, *name_len, GFP_KERNEL);
222+
if (!str)
223+
return ERR_PTR(-ENOMEM);
223224
/* Skip initial '_' */
224-
name++;
225-
name_end = strrchr(name, '_');
225+
str++;
226+
name_end = strrchr(str, '_');
226227
if (!name_end) {
227-
doutc(cl, "failed to parse long snapshot name: %s\n", name);
228+
doutc(cl, "failed to parse long snapshot name: %s\n", str);
228229
return ERR_PTR(-EIO);
229230
}
230-
*name_len = (name_end - name);
231+
*name_len = (name_end - str);
231232
if (*name_len <= 0) {
232233
pr_err_client(cl, "failed to parse long snapshot name\n");
233234
return ERR_PTR(-EIO);
234235
}
235236

236237
/* Get the inode number */
237-
inode_number = kmemdup_nul(name_end + 1,
238-
orig_len - *name_len - 2,
239-
GFP_KERNEL);
240-
if (!inode_number)
241-
return ERR_PTR(-ENOMEM);
238+
inode_number = name_end + 1;
242239
ret = kstrtou64(inode_number, 10, &vino.ino);
243240
if (ret) {
244-
doutc(cl, "failed to parse inode number: %s\n", name);
245-
dir = ERR_PTR(ret);
246-
goto out;
241+
doutc(cl, "failed to parse inode number: %s\n", str);
242+
return ERR_PTR(ret);
247243
}
248244

249245
/* And finally the inode */
@@ -254,50 +250,37 @@ static struct inode *parse_longname(const struct inode *parent,
254250
if (IS_ERR(dir))
255251
doutc(cl, "can't find inode %s (%s)\n", inode_number, name);
256252
}
257-
258-
out:
259-
kfree(inode_number);
260253
return dir;
261254
}
262255

263-
int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
264-
char *buf)
256+
int ceph_encode_encrypted_dname(struct inode *parent, char *buf, int elen)
265257
{
266258
struct ceph_client *cl = ceph_inode_to_client(parent);
267259
struct inode *dir = parent;
268-
struct qstr iname;
260+
char *p = buf;
269261
u32 len;
270-
int name_len;
271-
int elen;
262+
int name_len = elen;
272263
int ret;
273264
u8 *cryptbuf = NULL;
274265

275-
iname.name = d_name->name;
276-
name_len = d_name->len;
277-
278266
/* Handle the special case of snapshot names that start with '_' */
279-
if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
280-
(iname.name[0] == '_')) {
281-
dir = parse_longname(parent, iname.name, &name_len);
267+
if (ceph_snap(dir) == CEPH_SNAPDIR && *p == '_') {
268+
dir = parse_longname(parent, p, &name_len);
282269
if (IS_ERR(dir))
283270
return PTR_ERR(dir);
284-
iname.name++; /* skip initial '_' */
271+
p++; /* skip initial '_' */
285272
}
286-
iname.len = name_len;
287273

288-
if (!fscrypt_has_encryption_key(dir)) {
289-
memcpy(buf, d_name->name, d_name->len);
290-
elen = d_name->len;
274+
if (!fscrypt_has_encryption_key(dir))
291275
goto out;
292-
}
293276

294277
/*
295278
* Convert cleartext d_name to ciphertext. If result is longer than
296279
* CEPH_NOHASH_NAME_MAX, sha256 the remaining bytes
297280
*
298281
* See: fscrypt_setup_filename
299282
*/
300-
if (!fscrypt_fname_encrypted_size(dir, iname.len, NAME_MAX, &len)) {
283+
if (!fscrypt_fname_encrypted_size(dir, name_len, NAME_MAX, &len)) {
301284
elen = -ENAMETOOLONG;
302285
goto out;
303286
}
@@ -310,7 +293,9 @@ int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
310293
goto out;
311294
}
312295

313-
ret = fscrypt_fname_encrypt(dir, &iname, cryptbuf, len);
296+
ret = fscrypt_fname_encrypt(dir,
297+
&(struct qstr)QSTR_INIT(p, name_len),
298+
cryptbuf, len);
314299
if (ret) {
315300
elen = ret;
316301
goto out;
@@ -331,18 +316,13 @@ int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
331316
}
332317

333318
/* base64 encode the encrypted name */
334-
elen = ceph_base64_encode(cryptbuf, len, buf);
335-
doutc(cl, "base64-encoded ciphertext name = %.*s\n", elen, buf);
319+
elen = ceph_base64_encode(cryptbuf, len, p);
320+
doutc(cl, "base64-encoded ciphertext name = %.*s\n", elen, p);
336321

337322
/* To understand the 240 limit, see CEPH_NOHASH_NAME_MAX comments */
338323
WARN_ON(elen > 240);
339-
if ((elen > 0) && (dir != parent)) {
340-
char tmp_buf[NAME_MAX];
341-
342-
elen = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
343-
elen, buf, dir->i_ino);
344-
memcpy(buf, tmp_buf, elen);
345-
}
324+
if (dir != parent) // leading _ is already there; append _<inum>
325+
elen += 1 + sprintf(p + elen, "_%ld", dir->i_ino);
346326

347327
out:
348328
kfree(cryptbuf);
@@ -355,14 +335,6 @@ int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
355335
return elen;
356336
}
357337

358-
int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
359-
char *buf)
360-
{
361-
WARN_ON_ONCE(!fscrypt_has_encryption_key(parent));
362-
363-
return ceph_encode_encrypted_dname(parent, &dentry->d_name, buf);
364-
}
365-
366338
/**
367339
* ceph_fname_to_usr - convert a filename for userland presentation
368340
* @fname: ceph_fname to be converted

fs/ceph/crypto.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
102102
struct ceph_acl_sec_ctx *as);
103103
void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
104104
struct ceph_acl_sec_ctx *as);
105-
int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
106-
char *buf);
107-
int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
108-
char *buf);
105+
int ceph_encode_encrypted_dname(struct inode *parent, char *buf, int len);
109106

110107
static inline int ceph_fname_alloc_buffer(struct inode *parent,
111108
struct fscrypt_str *fname)
@@ -194,17 +191,10 @@ static inline void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
194191
{
195192
}
196193

197-
static inline int ceph_encode_encrypted_dname(struct inode *parent,
198-
struct qstr *d_name, char *buf)
194+
static inline int ceph_encode_encrypted_dname(struct inode *parent, char *buf,
195+
int len)
199196
{
200-
memcpy(buf, d_name->name, d_name->len);
201-
return d_name->len;
202-
}
203-
204-
static inline int ceph_encode_encrypted_fname(struct inode *parent,
205-
struct dentry *dentry, char *buf)
206-
{
207-
return -EOPNOTSUPP;
197+
return len;
208198
}
209199

210200
static inline int ceph_fname_alloc_buffer(struct inode *parent,

fs/ceph/dir.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,16 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
423423
req->r_inode_drop = CEPH_CAP_FILE_EXCL;
424424
}
425425
if (dfi->last_name) {
426-
struct qstr d_name = { .name = dfi->last_name,
427-
.len = strlen(dfi->last_name) };
426+
int len = strlen(dfi->last_name);
428427

429428
req->r_path2 = kzalloc(NAME_MAX + 1, GFP_KERNEL);
430429
if (!req->r_path2) {
431430
ceph_mdsc_put_request(req);
432431
return -ENOMEM;
433432
}
433+
memcpy(req->r_path2, dfi->last_name, len);
434434

435-
err = ceph_encode_encrypted_dname(inode, &d_name,
436-
req->r_path2);
435+
err = ceph_encode_encrypted_dname(inode, req->r_path2, len);
437436
if (err < 0) {
438437
ceph_mdsc_put_request(req);
439438
return err;

fs/ceph/mds_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,8 +2766,8 @@ char *ceph_mdsc_build_path(struct ceph_mds_client *mdsc, struct dentry *dentry,
27662766
}
27672767

27682768
if (fscrypt_has_encryption_key(d_inode(parent))) {
2769-
len = ceph_encode_encrypted_fname(d_inode(parent),
2770-
cur, buf);
2769+
len = ceph_encode_encrypted_dname(d_inode(parent),
2770+
buf, len);
27712771
if (len < 0) {
27722772
dput(parent);
27732773
dput(cur);

0 commit comments

Comments
 (0)