Skip to content

Commit ed94f87

Browse files
jtlaytonAl Viro
authored andcommitted
ceph: don't allow type or device number to change on non-I_NEW inodes
Al pointed out that a malicious or broken MDS could change the type or device number of a given inode number. It may also be possible for the MDS to reuse an old inode number. Ensure that we never allow fill_inode to change the type part of the i_mode or the i_rdev unless I_NEW is set. Throw warnings if the MDS ever changes these on us mid-stream, and return an error. Don't set i_rdev directly, and rely on init_special_inode to do it. Also, fix up error handling in the callers of ceph_get_inode. In handle_cap_grant, check for and warn if the inode type changes, and only overwrite the mode if it didn't. Reported-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 3e10a15 commit ed94f87

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

fs/ceph/caps.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3358,7 +3358,13 @@ static void handle_cap_grant(struct inode *inode,
33583358

33593359
if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
33603360
(extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3361-
inode->i_mode = le32_to_cpu(grant->mode);
3361+
umode_t mode = le32_to_cpu(grant->mode);
3362+
3363+
if (inode_wrong_type(inode, mode))
3364+
pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
3365+
ceph_vinop(inode), inode->i_mode, mode);
3366+
else
3367+
inode->i_mode = mode;
33623368
inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
33633369
inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
33643370
ci->i_btime = extra_info->btime;

fs/ceph/inode.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,32 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page,
769769
bool queue_trunc = false;
770770
bool new_version = false;
771771
bool fill_inline = false;
772+
umode_t mode = le32_to_cpu(info->mode);
773+
dev_t rdev = le32_to_cpu(info->rdev);
772774

773775
dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__,
774776
inode, ceph_vinop(inode), le64_to_cpu(info->version),
775777
ci->i_version);
776778

779+
/* Once I_NEW is cleared, we can't change type or dev numbers */
780+
if (inode->i_state & I_NEW) {
781+
inode->i_mode = mode;
782+
} else {
783+
if (inode_wrong_type(inode, mode)) {
784+
pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
785+
ceph_vinop(inode), inode->i_mode, mode);
786+
return -ESTALE;
787+
}
788+
789+
if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
790+
pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
791+
ceph_vinop(inode), MAJOR(inode->i_rdev),
792+
MINOR(inode->i_rdev), MAJOR(rdev),
793+
MINOR(rdev));
794+
return -ESTALE;
795+
}
796+
}
797+
777798
info_caps = le32_to_cpu(info->cap.caps);
778799

779800
/* prealloc new cap struct */
@@ -827,8 +848,6 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page,
827848
issued |= __ceph_caps_dirty(ci);
828849
new_issued = ~issued & info_caps;
829850

830-
/* update inode */
831-
inode->i_rdev = le32_to_cpu(info->rdev);
832851
/* directories have fl_stripe_unit set to zero */
833852
if (le32_to_cpu(info->layout.fl_stripe_unit))
834853
inode->i_blkbits =
@@ -840,7 +859,7 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page,
840859

841860
if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
842861
(issued & CEPH_CAP_AUTH_EXCL) == 0) {
843-
inode->i_mode = le32_to_cpu(info->mode);
862+
inode->i_mode = mode;
844863
inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
845864
inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
846865
dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
@@ -938,7 +957,7 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page,
938957
case S_IFCHR:
939958
case S_IFSOCK:
940959
inode->i_blkbits = PAGE_SHIFT;
941-
init_special_inode(inode, inode->i_mode, inode->i_rdev);
960+
init_special_inode(inode, inode->i_mode, rdev);
942961
inode->i_op = &ceph_file_iops;
943962
break;
944963
case S_IFREG:

0 commit comments

Comments
 (0)