Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ceph: add support for encrypted snapshot names

Since filenames in encrypted directories are encrypted and shown as
a base64-encoded string when the directory is locked, make snapshot
names show a similar behaviour.

When creating a snapshot, .snap directories for every subdirectory will
show the snapshot name in the "long format":

# mkdir .snap/my-snap
# ls my-dir/.snap/
_my-snap_1099511627782

Encrypted snapshots will need to be able to handle these by
encrypting/decrypting only the snapshot part of the string ('my-snap').

Also, since the MDS prevents snapshot names to be bigger than 240
characters it is necessary to adapt CEPH_NOHASH_NAME_MAX to accommodate
this extra limitation.

[ idryomov: drop const on !CONFIG_FS_ENCRYPTION branch too ]

Signed-off-by: Luís Henriques <lhenriques@suse.de>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
Reviewed-by: Milind Changire <mchangir@redhat.com>
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>

authored by

Luís Henriques and committed by
Ilya Dryomov
dd66df00 b422f115

+201 -41
+166 -31
fs/ceph/crypto.c
··· 193 193 swap(req->r_fscrypt_auth, as->fscrypt_auth); 194 194 } 195 195 196 - int ceph_encode_encrypted_dname(const struct inode *parent, 197 - struct qstr *d_name, char *buf) 196 + /* 197 + * User-created snapshots can't start with '_'. Snapshots that start with this 198 + * character are special (hint: there aren't real snapshots) and use the 199 + * following format: 200 + * 201 + * _<SNAPSHOT-NAME>_<INODE-NUMBER> 202 + * 203 + * where: 204 + * - <SNAPSHOT-NAME> - the real snapshot name that may need to be decrypted, 205 + * - <INODE-NUMBER> - the inode number (in decimal) for the actual snapshot 206 + * 207 + * This function parses these snapshot names and returns the inode 208 + * <INODE-NUMBER>. 'name_len' will also bet set with the <SNAPSHOT-NAME> 209 + * length. 210 + */ 211 + static struct inode *parse_longname(const struct inode *parent, 212 + const char *name, int *name_len) 198 213 { 214 + struct inode *dir = NULL; 215 + struct ceph_vino vino = { .snap = CEPH_NOSNAP }; 216 + char *inode_number; 217 + char *name_end; 218 + int orig_len = *name_len; 219 + int ret = -EIO; 220 + 221 + /* Skip initial '_' */ 222 + name++; 223 + name_end = strrchr(name, '_'); 224 + if (!name_end) { 225 + dout("Failed to parse long snapshot name: %s\n", name); 226 + return ERR_PTR(-EIO); 227 + } 228 + *name_len = (name_end - name); 229 + if (*name_len <= 0) { 230 + pr_err("Failed to parse long snapshot name\n"); 231 + return ERR_PTR(-EIO); 232 + } 233 + 234 + /* Get the inode number */ 235 + inode_number = kmemdup_nul(name_end + 1, 236 + orig_len - *name_len - 2, 237 + GFP_KERNEL); 238 + if (!inode_number) 239 + return ERR_PTR(-ENOMEM); 240 + ret = kstrtou64(inode_number, 10, &vino.ino); 241 + if (ret) { 242 + dout("Failed to parse inode number: %s\n", name); 243 + dir = ERR_PTR(ret); 244 + goto out; 245 + } 246 + 247 + /* And finally the inode */ 248 + dir = ceph_find_inode(parent->i_sb, vino); 249 + if (!dir) { 250 + /* This can happen if we're not mounting cephfs on the root */ 251 + dir = ceph_get_inode(parent->i_sb, vino, NULL); 252 + if (!dir) 253 + dir = ERR_PTR(-ENOENT); 254 + } 255 + if (IS_ERR(dir)) 256 + dout("Can't find inode %s (%s)\n", inode_number, name); 257 + 258 + out: 259 + kfree(inode_number); 260 + return dir; 261 + } 262 + 263 + int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name, 264 + char *buf) 265 + { 266 + struct inode *dir = parent; 267 + struct qstr iname; 199 268 u32 len; 269 + int name_len; 200 270 int elen; 201 271 int ret; 202 - u8 *cryptbuf; 272 + u8 *cryptbuf = NULL; 203 273 204 - if (!fscrypt_has_encryption_key(parent)) { 274 + iname.name = d_name->name; 275 + name_len = d_name->len; 276 + 277 + /* Handle the special case of snapshot names that start with '_' */ 278 + if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) && 279 + (iname.name[0] == '_')) { 280 + dir = parse_longname(parent, iname.name, &name_len); 281 + if (IS_ERR(dir)) 282 + return PTR_ERR(dir); 283 + iname.name++; /* skip initial '_' */ 284 + } 285 + iname.len = name_len; 286 + 287 + if (!fscrypt_has_encryption_key(dir)) { 205 288 memcpy(buf, d_name->name, d_name->len); 206 - return d_name->len; 289 + elen = d_name->len; 290 + goto out; 207 291 } 208 292 209 293 /* ··· 296 212 * 297 213 * See: fscrypt_setup_filename 298 214 */ 299 - if (!fscrypt_fname_encrypted_size(parent, d_name->len, NAME_MAX, &len)) 300 - return -ENAMETOOLONG; 215 + if (!fscrypt_fname_encrypted_size(dir, iname.len, NAME_MAX, &len)) { 216 + elen = -ENAMETOOLONG; 217 + goto out; 218 + } 301 219 302 220 /* Allocate a buffer appropriate to hold the result */ 303 221 cryptbuf = kmalloc(len > CEPH_NOHASH_NAME_MAX ? NAME_MAX : len, 304 222 GFP_KERNEL); 305 - if (!cryptbuf) 306 - return -ENOMEM; 223 + if (!cryptbuf) { 224 + elen = -ENOMEM; 225 + goto out; 226 + } 307 227 308 - ret = fscrypt_fname_encrypt(parent, d_name, cryptbuf, len); 228 + ret = fscrypt_fname_encrypt(dir, &iname, cryptbuf, len); 309 229 if (ret) { 310 - kfree(cryptbuf); 311 - return ret; 230 + elen = ret; 231 + goto out; 312 232 } 313 233 314 234 /* hash the end if the name is long enough */ ··· 331 243 332 244 /* base64 encode the encrypted name */ 333 245 elen = ceph_base64_encode(cryptbuf, len, buf); 334 - kfree(cryptbuf); 335 246 dout("base64-encoded ciphertext name = %.*s\n", elen, buf); 247 + 248 + /* To understand the 240 limit, see CEPH_NOHASH_NAME_MAX comments */ 249 + WARN_ON(elen > 240); 250 + if ((elen > 0) && (dir != parent)) { 251 + char tmp_buf[NAME_MAX]; 252 + 253 + elen = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld", 254 + elen, buf, dir->i_ino); 255 + memcpy(buf, tmp_buf, elen); 256 + } 257 + 258 + out: 259 + kfree(cryptbuf); 260 + if (dir != parent) { 261 + if ((dir->i_state & I_NEW)) 262 + discard_new_inode(dir); 263 + else 264 + iput(dir); 265 + } 336 266 return elen; 337 267 } 338 268 339 - int ceph_encode_encrypted_fname(const struct inode *parent, 340 - struct dentry *dentry, char *buf) 269 + int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry, 270 + char *buf) 341 271 { 342 272 WARN_ON_ONCE(!fscrypt_has_encryption_key(parent)); 343 273 ··· 380 274 int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, 381 275 struct fscrypt_str *oname, bool *is_nokey) 382 276 { 383 - int ret; 277 + struct inode *dir = fname->dir; 384 278 struct fscrypt_str _tname = FSTR_INIT(NULL, 0); 385 279 struct fscrypt_str iname; 386 - 387 - if (!IS_ENCRYPTED(fname->dir)) { 388 - oname->name = fname->name; 389 - oname->len = fname->name_len; 390 - return 0; 391 - } 280 + char *name = fname->name; 281 + int name_len = fname->name_len; 282 + int ret; 392 283 393 284 /* Sanity check that the resulting name will fit in the buffer */ 394 285 if (fname->name_len > NAME_MAX || fname->ctext_len > NAME_MAX) 395 286 return -EIO; 396 287 397 - ret = ceph_fscrypt_prepare_readdir(fname->dir); 398 - if (ret < 0) 399 - return ret; 288 + /* Handle the special case of snapshot names that start with '_' */ 289 + if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) && 290 + (name[0] == '_')) { 291 + dir = parse_longname(dir, name, &name_len); 292 + if (IS_ERR(dir)) 293 + return PTR_ERR(dir); 294 + name++; /* skip initial '_' */ 295 + } 296 + 297 + if (!IS_ENCRYPTED(dir)) { 298 + oname->name = fname->name; 299 + oname->len = fname->name_len; 300 + ret = 0; 301 + goto out_inode; 302 + } 303 + 304 + ret = ceph_fscrypt_prepare_readdir(dir); 305 + if (ret) 306 + goto out_inode; 400 307 401 308 /* 402 309 * Use the raw dentry name as sent by the MDS instead of 403 310 * generating a nokey name via fscrypt. 404 311 */ 405 - if (!fscrypt_has_encryption_key(fname->dir)) { 312 + if (!fscrypt_has_encryption_key(dir)) { 406 313 if (fname->no_copy) 407 314 oname->name = fname->name; 408 315 else ··· 423 304 oname->len = fname->name_len; 424 305 if (is_nokey) 425 306 *is_nokey = true; 426 - return 0; 307 + ret = 0; 308 + goto out_inode; 427 309 } 428 310 429 311 if (fname->ctext_len == 0) { ··· 433 313 if (!tname) { 434 314 ret = fscrypt_fname_alloc_buffer(NAME_MAX, &_tname); 435 315 if (ret) 436 - return ret; 316 + goto out_inode; 437 317 tname = &_tname; 438 318 } 439 319 440 - declen = ceph_base64_decode(fname->name, fname->name_len, 441 - tname->name); 320 + declen = ceph_base64_decode(name, name_len, tname->name); 442 321 if (declen <= 0) { 443 322 ret = -EIO; 444 323 goto out; ··· 449 330 iname.len = fname->ctext_len; 450 331 } 451 332 452 - ret = fscrypt_fname_disk_to_usr(fname->dir, 0, 0, &iname, oname); 333 + ret = fscrypt_fname_disk_to_usr(dir, 0, 0, &iname, oname); 334 + if (!ret && (dir != fname->dir)) { 335 + char tmp_buf[CEPH_BASE64_CHARS(NAME_MAX)]; 336 + 337 + name_len = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld", 338 + oname->len, oname->name, dir->i_ino); 339 + memcpy(oname->name, tmp_buf, name_len); 340 + oname->len = name_len; 341 + } 342 + 453 343 out: 454 344 fscrypt_fname_free_buffer(&_tname); 345 + out_inode: 346 + if ((dir != fname->dir) && !IS_ERR(dir)) { 347 + if ((dir->i_state & I_NEW)) 348 + discard_new_inode(dir); 349 + else 350 + iput(dir); 351 + } 455 352 return ret; 456 353 } 457 354
+6 -6
fs/ceph/crypto.h
··· 102 102 struct ceph_acl_sec_ctx *as); 103 103 void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req, 104 104 struct ceph_acl_sec_ctx *as); 105 - int ceph_encode_encrypted_dname(const struct inode *parent, 106 - struct qstr *d_name, char *buf); 107 - int ceph_encode_encrypted_fname(const struct inode *parent, 108 - struct dentry *dentry, char *buf); 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); 109 109 110 110 static inline int ceph_fname_alloc_buffer(struct inode *parent, 111 111 struct fscrypt_str *fname) ··· 194 194 { 195 195 } 196 196 197 - static inline int ceph_encode_encrypted_dname(const struct inode *parent, 197 + static inline int ceph_encode_encrypted_dname(struct inode *parent, 198 198 struct qstr *d_name, char *buf) 199 199 { 200 200 memcpy(buf, d_name->name, d_name->len); 201 201 return d_name->len; 202 202 } 203 203 204 - static inline int ceph_encode_encrypted_fname(const struct inode *parent, 204 + static inline int ceph_encode_encrypted_fname(struct inode *parent, 205 205 struct dentry *dentry, char *buf) 206 206 { 207 207 return -EOPNOTSUPP;
+29 -4
fs/ceph/inode.c
··· 91 91 if (err < 0) 92 92 goto out_err; 93 93 94 - err = ceph_fscrypt_prepare_context(dir, inode, as_ctx); 95 - if (err) 96 - goto out_err; 94 + /* 95 + * We'll skip setting fscrypt context for snapshots, leaving that for 96 + * the handle_reply(). 97 + */ 98 + if (ceph_snap(dir) != CEPH_SNAPDIR) { 99 + err = ceph_fscrypt_prepare_context(dir, inode, as_ctx); 100 + if (err) 101 + goto out_err; 102 + } 97 103 98 104 return inode; 99 105 out_err: ··· 165 159 }; 166 160 struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL); 167 161 struct ceph_inode_info *ci = ceph_inode(inode); 162 + int ret = -ENOTDIR; 168 163 169 164 if (IS_ERR(inode)) 170 165 return inode; ··· 191 184 ci->i_rbytes = 0; 192 185 ci->i_btime = ceph_inode(parent)->i_btime; 193 186 187 + #ifdef CONFIG_FS_ENCRYPTION 188 + /* if encrypted, just borrow fscrypt_auth from parent */ 189 + if (IS_ENCRYPTED(parent)) { 190 + struct ceph_inode_info *pci = ceph_inode(parent); 191 + 192 + ci->fscrypt_auth = kmemdup(pci->fscrypt_auth, 193 + pci->fscrypt_auth_len, 194 + GFP_KERNEL); 195 + if (ci->fscrypt_auth) { 196 + inode->i_flags |= S_ENCRYPTED; 197 + ci->fscrypt_auth_len = pci->fscrypt_auth_len; 198 + } else { 199 + dout("Failed to alloc snapdir fscrypt_auth\n"); 200 + ret = -ENOMEM; 201 + goto err; 202 + } 203 + } 204 + #endif 194 205 if (inode->i_state & I_NEW) { 195 206 inode->i_op = &ceph_snapdir_iops; 196 207 inode->i_fop = &ceph_snapdir_fops; ··· 222 197 discard_new_inode(inode); 223 198 else 224 199 iput(inode); 225 - return ERR_PTR(-ENOTDIR); 200 + return ERR_PTR(ret); 226 201 } 227 202 228 203 const struct inode_operations ceph_file_iops = {