Skip to content

Commit ca7f85b

Browse files
ubifs: Add support for encrypted symlinks
Signed-off-by: Richard Weinberger <[email protected]>
1 parent f4f61d2 commit ca7f85b

File tree

3 files changed

+126
-13
lines changed

3 files changed

+126
-13
lines changed

fs/ubifs/dir.c

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
629629
fstr.len = fstr_real_len;
630630

631631
err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c, &dent->key), 0, &nm.disk_name, &fstr);
632-
if (err < 0)
632+
if (err)
633633
goto out;
634634
} else {
635635
fstr.len = fname_len(&nm);
@@ -1164,10 +1164,27 @@ static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
11641164
struct ubifs_inode *dir_ui = ubifs_inode(dir);
11651165
struct ubifs_info *c = dir->i_sb->s_fs_info;
11661166
int err, len = strlen(symname);
1167-
int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
1167+
int sz_change = CALC_DENT_SIZE(len);
1168+
struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
1169+
struct fscrypt_symlink_data *sd = NULL;
11681170
struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
11691171
.new_ino_d = ALIGN(len, 8),
11701172
.dirtied_ino = 1 };
1173+
struct fscrypt_name nm;
1174+
1175+
if (ubifs_crypt_is_encrypted(dir)) {
1176+
err = fscrypt_get_encryption_info(dir);
1177+
if (err)
1178+
goto out_budg;
1179+
1180+
if (!fscrypt_has_encryption_key(dir)) {
1181+
err = -EPERM;
1182+
goto out_budg;
1183+
}
1184+
1185+
disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
1186+
sizeof(struct fscrypt_symlink_data));
1187+
}
11711188

11721189
/*
11731190
* Budget request settings: new inode, new direntry and changing parent
@@ -1177,36 +1194,77 @@ static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
11771194
dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
11781195
symname, dir->i_ino);
11791196

1180-
if (len > UBIFS_MAX_INO_DATA)
1197+
if (disk_link.len > UBIFS_MAX_INO_DATA)
11811198
return -ENAMETOOLONG;
11821199

11831200
err = ubifs_budget_space(c, &req);
11841201
if (err)
11851202
return err;
11861203

1204+
err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1205+
if (err)
1206+
goto out_budg;
1207+
11871208
inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
11881209
if (IS_ERR(inode)) {
11891210
err = PTR_ERR(inode);
1190-
goto out_budg;
1211+
goto out_fname;
11911212
}
11921213

11931214
ui = ubifs_inode(inode);
1194-
ui->data = kmalloc(len + 1, GFP_NOFS);
1215+
ui->data = kmalloc(disk_link.len, GFP_NOFS);
11951216
if (!ui->data) {
11961217
err = -ENOMEM;
11971218
goto out_inode;
11981219
}
11991220

1200-
memcpy(ui->data, symname, len);
1201-
((char *)ui->data)[len] = '\0';
1202-
inode->i_link = ui->data;
1221+
if (ubifs_crypt_is_encrypted(dir)) {
1222+
struct qstr istr = QSTR_INIT(symname, len);
1223+
struct fscrypt_str ostr;
1224+
1225+
sd = kzalloc(disk_link.len, GFP_NOFS);
1226+
if (!sd) {
1227+
err = -ENOMEM;
1228+
goto out_inode;
1229+
}
1230+
1231+
err = fscrypt_get_encryption_info(inode);
1232+
if (err) {
1233+
kfree(sd);
1234+
goto out_inode;
1235+
}
1236+
1237+
if (!fscrypt_has_encryption_key(inode)) {
1238+
kfree(sd);
1239+
err = -EPERM;
1240+
goto out_inode;
1241+
}
1242+
1243+
ostr.name = sd->encrypted_path;
1244+
ostr.len = disk_link.len;
1245+
1246+
err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
1247+
if (err) {
1248+
kfree(sd);
1249+
goto out_inode;
1250+
}
1251+
1252+
sd->len = cpu_to_le16(ostr.len);
1253+
disk_link.name = (char *)sd;
1254+
} else {
1255+
inode->i_link = ui->data;
1256+
}
1257+
1258+
memcpy(ui->data, disk_link.name, disk_link.len);
1259+
((char *)ui->data)[disk_link.len - 1] = '\0';
1260+
12031261
/*
12041262
* The terminating zero byte is not written to the flash media and it
12051263
* is put just to make later in-memory string processing simpler. Thus,
12061264
* data length is @len, not @len + %1.
12071265
*/
1208-
ui->data_len = len;
1209-
inode->i_size = ubifs_inode(inode)->ui_size = len;
1266+
ui->data_len = disk_link.len - 1;
1267+
inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
12101268

12111269
err = ubifs_init_security(dir, inode, &dentry->d_name);
12121270
if (err)
@@ -1216,14 +1274,15 @@ static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
12161274
dir->i_size += sz_change;
12171275
dir_ui->ui_size = dir->i_size;
12181276
dir->i_mtime = dir->i_ctime = inode->i_ctime;
1219-
err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
1277+
err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
12201278
if (err)
12211279
goto out_cancel;
12221280
mutex_unlock(&dir_ui->ui_mutex);
12231281

12241282
ubifs_release_budget(c, &req);
12251283
insert_inode_hash(inode);
12261284
d_instantiate(dentry, inode);
1285+
fscrypt_free_filename(&nm);
12271286
return 0;
12281287

12291288
out_cancel:
@@ -1233,6 +1292,8 @@ static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
12331292
out_inode:
12341293
make_bad_inode(inode);
12351294
iput(inode);
1295+
out_fname:
1296+
fscrypt_free_filename(&nm);
12361297
out_budg:
12371298
ubifs_release_budget(c, &req);
12381299
return err;

fs/ubifs/file.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,59 @@ static int ubifs_file_open(struct inode *inode, struct file *filp)
16571657
return 0;
16581658
}
16591659

1660+
static const char *ubifs_get_link(struct dentry *dentry,
1661+
struct inode *inode,
1662+
struct delayed_call *done)
1663+
{
1664+
int err;
1665+
struct fscrypt_symlink_data *sd;
1666+
struct ubifs_inode *ui = ubifs_inode(inode);
1667+
struct fscrypt_str cstr;
1668+
struct fscrypt_str pstr;
1669+
1670+
if (!ubifs_crypt_is_encrypted(inode))
1671+
return ui->data;
1672+
1673+
if (!dentry)
1674+
return ERR_PTR(-ECHILD);
1675+
1676+
err = fscrypt_get_encryption_info(inode);
1677+
if (err)
1678+
return ERR_PTR(err);
1679+
1680+
sd = (struct fscrypt_symlink_data *)ui->data;
1681+
cstr.name = sd->encrypted_path;
1682+
cstr.len = le16_to_cpu(sd->len);
1683+
1684+
if (cstr.len == 0)
1685+
return ERR_PTR(-ENOENT);
1686+
1687+
if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > ui->data_len)
1688+
return ERR_PTR(-EIO);
1689+
1690+
err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
1691+
if (err)
1692+
return ERR_PTR(err);
1693+
1694+
err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1695+
if (err) {
1696+
fscrypt_fname_free_buffer(&pstr);
1697+
return ERR_PTR(err);
1698+
}
1699+
1700+
pstr.name[pstr.len] = '\0';
1701+
1702+
// XXX this probably won't happen anymore...
1703+
if (pstr.name[0] == '\0') {
1704+
fscrypt_fname_free_buffer(&pstr);
1705+
return ERR_PTR(-ENOENT);
1706+
}
1707+
1708+
set_delayed_call(done, kfree_link, pstr.name);
1709+
return pstr.name;
1710+
}
1711+
1712+
16601713
const struct address_space_operations ubifs_file_address_operations = {
16611714
.readpage = ubifs_readpage,
16621715
.writepage = ubifs_writepage,
@@ -1681,7 +1734,7 @@ const struct inode_operations ubifs_file_inode_operations = {
16811734

16821735
const struct inode_operations ubifs_symlink_inode_operations = {
16831736
.readlink = generic_readlink,
1684-
.get_link = simple_get_link,
1737+
.get_link = ubifs_get_link,
16851738
.setattr = ubifs_setattr,
16861739
.getattr = ubifs_getattr,
16871740
.listxattr = ubifs_listxattr,

fs/ubifs/super.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
198198
}
199199
memcpy(ui->data, ino->data, ui->data_len);
200200
((char *)ui->data)[ui->data_len] = '\0';
201-
inode->i_link = ui->data;
202201
break;
203202
case S_IFBLK:
204203
case S_IFCHR:

0 commit comments

Comments
 (0)