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

libceph: make authorizer destruction independent of ceph_auth_client

Starting the kernel client with cephx disabled and then enabling cephx
and restarting userspace daemons can result in a crash:

[262671.478162] BUG: unable to handle kernel paging request at ffffebe000000000
[262671.531460] IP: [<ffffffff811cd04a>] kfree+0x5a/0x130
[262671.584334] PGD 0
[262671.635847] Oops: 0000 [#1] SMP
[262672.055841] CPU: 22 PID: 2961272 Comm: kworker/22:2 Not tainted 4.2.0-34-generic #39~14.04.1-Ubuntu
[262672.162338] Hardware name: Dell Inc. PowerEdge R720/068CDY, BIOS 2.4.3 07/09/2014
[262672.268937] Workqueue: ceph-msgr con_work [libceph]
[262672.322290] task: ffff88081c2d0dc0 ti: ffff880149ae8000 task.ti: ffff880149ae8000
[262672.428330] RIP: 0010:[<ffffffff811cd04a>] [<ffffffff811cd04a>] kfree+0x5a/0x130
[262672.535880] RSP: 0018:ffff880149aeba58 EFLAGS: 00010286
[262672.589486] RAX: 000001e000000000 RBX: 0000000000000012 RCX: ffff8807e7461018
[262672.695980] RDX: 000077ff80000000 RSI: ffff88081af2be04 RDI: 0000000000000012
[262672.803668] RBP: ffff880149aeba78 R08: 0000000000000000 R09: 0000000000000000
[262672.912299] R10: ffffebe000000000 R11: ffff880819a60e78 R12: ffff8800aec8df40
[262673.021769] R13: ffffffffc035f70f R14: ffff8807e5b138e0 R15: ffff880da9785840
[262673.131722] FS: 0000000000000000(0000) GS:ffff88081fac0000(0000) knlGS:0000000000000000
[262673.245377] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[262673.303281] CR2: ffffebe000000000 CR3: 0000000001c0d000 CR4: 00000000001406e0
[262673.417556] Stack:
[262673.472943] ffff880149aeba88 ffff88081af2be04 ffff8800aec8df40 ffff88081af2be04
[262673.583767] ffff880149aeba98 ffffffffc035f70f ffff880149aebac8 ffff8800aec8df00
[262673.694546] ffff880149aebac8 ffffffffc035c89e ffff8807e5b138e0 ffff8805b047f800
[262673.805230] Call Trace:
[262673.859116] [<ffffffffc035f70f>] ceph_x_destroy_authorizer+0x1f/0x50 [libceph]
[262673.968705] [<ffffffffc035c89e>] ceph_auth_destroy_authorizer+0x3e/0x60 [libceph]
[262674.078852] [<ffffffffc0352805>] put_osd+0x45/0x80 [libceph]
[262674.134249] [<ffffffffc035290e>] remove_osd+0xae/0x140 [libceph]
[262674.189124] [<ffffffffc0352aa3>] __reset_osd+0x103/0x150 [libceph]
[262674.243749] [<ffffffffc0354703>] kick_requests+0x223/0x460 [libceph]
[262674.297485] [<ffffffffc03559e2>] ceph_osdc_handle_map+0x282/0x5e0 [libceph]
[262674.350813] [<ffffffffc035022e>] dispatch+0x4e/0x720 [libceph]
[262674.403312] [<ffffffffc034bd91>] try_read+0x3d1/0x1090 [libceph]
[262674.454712] [<ffffffff810ab7c2>] ? dequeue_entity+0x152/0x690
[262674.505096] [<ffffffffc034cb1b>] con_work+0xcb/0x1300 [libceph]
[262674.555104] [<ffffffff8108fb3e>] process_one_work+0x14e/0x3d0
[262674.604072] [<ffffffff810901ea>] worker_thread+0x11a/0x470
[262674.652187] [<ffffffff810900d0>] ? rescuer_thread+0x310/0x310
[262674.699022] [<ffffffff810957a2>] kthread+0xd2/0xf0
[262674.744494] [<ffffffff810956d0>] ? kthread_create_on_node+0x1c0/0x1c0
[262674.789543] [<ffffffff817bd81f>] ret_from_fork+0x3f/0x70
[262674.834094] [<ffffffff810956d0>] ? kthread_create_on_node+0x1c0/0x1c0

What happens is the following:

(1) new MON session is established
(2) old "none" ac is destroyed
(3) new "cephx" ac is constructed
...
(4) old OSD session (w/ "none" authorizer) is put
ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer)

osd->o_auth.authorizer in the "none" case is just a bare pointer into
ac, which contains a single static copy for all services. By the time
we get to (4), "none" ac, freed in (2), is long gone. On top of that,
a new vtable installed in (3) points us at ceph_x_destroy_authorizer(),
so we end up trying to destroy a "none" authorizer with a "cephx"
destructor operating on invalid memory!

To fix this, decouple authorizer destruction from ac and do away with
a single static "none" authorizer by making a copy for each OSD or MDS
session. Authorizers themselves are independent of ac and so there is
no reason for destroy_authorizer() to be an ac op. Make it an op on
the authorizer itself by turning ceph_authorizer into a real struct.

Fixes: http://tracker.ceph.com/issues/15447

Reported-by: Alan Zhang <alan.zhang@linux.com>
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Reviewed-by: Sage Weil <sage@redhat.com>

+62 -65
+2 -4
fs/ceph/mds_client.c
··· 386 386 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1); 387 387 if (atomic_dec_and_test(&s->s_ref)) { 388 388 if (s->s_auth.authorizer) 389 - ceph_auth_destroy_authorizer( 390 - s->s_mdsc->fsc->client->monc.auth, 391 - s->s_auth.authorizer); 389 + ceph_auth_destroy_authorizer(s->s_auth.authorizer); 392 390 kfree(s); 393 391 } 394 392 } ··· 3898 3900 struct ceph_auth_handshake *auth = &s->s_auth; 3899 3901 3900 3902 if (force_new && auth->authorizer) { 3901 - ceph_auth_destroy_authorizer(ac, auth->authorizer); 3903 + ceph_auth_destroy_authorizer(auth->authorizer); 3902 3904 auth->authorizer = NULL; 3903 3905 } 3904 3906 if (!auth->authorizer) {
+5 -5
include/linux/ceph/auth.h
··· 12 12 */ 13 13 14 14 struct ceph_auth_client; 15 - struct ceph_authorizer; 16 15 struct ceph_msg; 16 + 17 + struct ceph_authorizer { 18 + void (*destroy)(struct ceph_authorizer *); 19 + }; 17 20 18 21 struct ceph_auth_handshake { 19 22 struct ceph_authorizer *authorizer; ··· 65 62 struct ceph_auth_handshake *auth); 66 63 int (*verify_authorizer_reply)(struct ceph_auth_client *ac, 67 64 struct ceph_authorizer *a, size_t len); 68 - void (*destroy_authorizer)(struct ceph_auth_client *ac, 69 - struct ceph_authorizer *a); 70 65 void (*invalidate_authorizer)(struct ceph_auth_client *ac, 71 66 int peer_type); 72 67 ··· 113 112 extern int ceph_auth_create_authorizer(struct ceph_auth_client *ac, 114 113 int peer_type, 115 114 struct ceph_auth_handshake *auth); 116 - extern void ceph_auth_destroy_authorizer(struct ceph_auth_client *ac, 117 - struct ceph_authorizer *a); 115 + void ceph_auth_destroy_authorizer(struct ceph_authorizer *a); 118 116 extern int ceph_auth_update_authorizer(struct ceph_auth_client *ac, 119 117 int peer_type, 120 118 struct ceph_auth_handshake *a);
-1
include/linux/ceph/osd_client.h
··· 16 16 struct ceph_snap_context; 17 17 struct ceph_osd_request; 18 18 struct ceph_osd_client; 19 - struct ceph_authorizer; 20 19 21 20 /* 22 21 * completion callback for async writepages
+2 -6
net/ceph/auth.c
··· 293 293 } 294 294 EXPORT_SYMBOL(ceph_auth_create_authorizer); 295 295 296 - void ceph_auth_destroy_authorizer(struct ceph_auth_client *ac, 297 - struct ceph_authorizer *a) 296 + void ceph_auth_destroy_authorizer(struct ceph_authorizer *a) 298 297 { 299 - mutex_lock(&ac->mutex); 300 - if (ac->ops && ac->ops->destroy_authorizer) 301 - ac->ops->destroy_authorizer(ac, a); 302 - mutex_unlock(&ac->mutex); 298 + a->destroy(a); 303 299 } 304 300 EXPORT_SYMBOL(ceph_auth_destroy_authorizer); 305 301
+39 -32
net/ceph/auth_none.c
··· 16 16 struct ceph_auth_none_info *xi = ac->private; 17 17 18 18 xi->starting = true; 19 - xi->built_authorizer = false; 20 19 } 21 20 22 21 static void destroy(struct ceph_auth_client *ac) ··· 38 39 return xi->starting; 39 40 } 40 41 42 + static int ceph_auth_none_build_authorizer(struct ceph_auth_client *ac, 43 + struct ceph_none_authorizer *au) 44 + { 45 + void *p = au->buf; 46 + void *const end = p + sizeof(au->buf); 47 + int ret; 48 + 49 + ceph_encode_8_safe(&p, end, 1, e_range); 50 + ret = ceph_entity_name_encode(ac->name, &p, end); 51 + if (ret < 0) 52 + return ret; 53 + 54 + ceph_encode_64_safe(&p, end, ac->global_id, e_range); 55 + au->buf_len = p - (void *)au->buf; 56 + dout("%s built authorizer len %d\n", __func__, au->buf_len); 57 + return 0; 58 + 59 + e_range: 60 + return -ERANGE; 61 + } 62 + 41 63 static int build_request(struct ceph_auth_client *ac, void *buf, void *end) 42 64 { 43 65 return 0; ··· 77 57 return result; 78 58 } 79 59 60 + static void ceph_auth_none_destroy_authorizer(struct ceph_authorizer *a) 61 + { 62 + kfree(a); 63 + } 64 + 80 65 /* 81 - * build an 'authorizer' with our entity_name and global_id. we can 82 - * reuse a single static copy since it is identical for all services 83 - * we connect to. 66 + * build an 'authorizer' with our entity_name and global_id. it is 67 + * identical for all services we connect to. 84 68 */ 85 69 static int ceph_auth_none_create_authorizer( 86 70 struct ceph_auth_client *ac, int peer_type, 87 71 struct ceph_auth_handshake *auth) 88 72 { 89 - struct ceph_auth_none_info *ai = ac->private; 90 - struct ceph_none_authorizer *au = &ai->au; 91 - void *p, *end; 73 + struct ceph_none_authorizer *au; 92 74 int ret; 93 75 94 - if (!ai->built_authorizer) { 95 - p = au->buf; 96 - end = p + sizeof(au->buf); 97 - ceph_encode_8(&p, 1); 98 - ret = ceph_entity_name_encode(ac->name, &p, end - 8); 99 - if (ret < 0) 100 - goto bad; 101 - ceph_decode_need(&p, end, sizeof(u64), bad2); 102 - ceph_encode_64(&p, ac->global_id); 103 - au->buf_len = p - (void *)au->buf; 104 - ai->built_authorizer = true; 105 - dout("built authorizer len %d\n", au->buf_len); 76 + au = kmalloc(sizeof(*au), GFP_NOFS); 77 + if (!au) 78 + return -ENOMEM; 79 + 80 + au->base.destroy = ceph_auth_none_destroy_authorizer; 81 + 82 + ret = ceph_auth_none_build_authorizer(ac, au); 83 + if (ret) { 84 + kfree(au); 85 + return ret; 106 86 } 107 87 108 88 auth->authorizer = (struct ceph_authorizer *) au; ··· 112 92 auth->authorizer_reply_buf_len = sizeof (au->reply_buf); 113 93 114 94 return 0; 115 - 116 - bad2: 117 - ret = -ERANGE; 118 - bad: 119 - return ret; 120 - } 121 - 122 - static void ceph_auth_none_destroy_authorizer(struct ceph_auth_client *ac, 123 - struct ceph_authorizer *a) 124 - { 125 - /* nothing to do */ 126 95 } 127 96 128 97 static const struct ceph_auth_client_ops ceph_auth_none_ops = { ··· 123 114 .build_request = build_request, 124 115 .handle_reply = handle_reply, 125 116 .create_authorizer = ceph_auth_none_create_authorizer, 126 - .destroy_authorizer = ceph_auth_none_destroy_authorizer, 127 117 }; 128 118 129 119 int ceph_auth_none_init(struct ceph_auth_client *ac) ··· 135 127 return -ENOMEM; 136 128 137 129 xi->starting = true; 138 - xi->built_authorizer = false; 139 130 140 131 ac->protocol = CEPH_AUTH_NONE; 141 132 ac->private = xi;
+1 -2
net/ceph/auth_none.h
··· 12 12 */ 13 13 14 14 struct ceph_none_authorizer { 15 + struct ceph_authorizer base; 15 16 char buf[128]; 16 17 int buf_len; 17 18 char reply_buf[0]; ··· 20 19 21 20 struct ceph_auth_none_info { 22 21 bool starting; 23 - bool built_authorizer; 24 - struct ceph_none_authorizer au; /* we only need one; it's static */ 25 22 }; 26 23 27 24 int ceph_auth_none_init(struct ceph_auth_client *ac);
+10 -11
net/ceph/auth_x.c
··· 565 565 return -EAGAIN; 566 566 } 567 567 568 + static void ceph_x_destroy_authorizer(struct ceph_authorizer *a) 569 + { 570 + struct ceph_x_authorizer *au = (void *)a; 571 + 572 + ceph_x_authorizer_cleanup(au); 573 + kfree(au); 574 + } 575 + 568 576 static int ceph_x_create_authorizer( 569 577 struct ceph_auth_client *ac, int peer_type, 570 578 struct ceph_auth_handshake *auth) ··· 588 580 au = kzalloc(sizeof(*au), GFP_NOFS); 589 581 if (!au) 590 582 return -ENOMEM; 583 + 584 + au->base.destroy = ceph_x_destroy_authorizer; 591 585 592 586 ret = ceph_x_build_authorizer(ac, th, au); 593 587 if (ret) { ··· 652 642 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret); 653 643 return ret; 654 644 } 655 - 656 - static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac, 657 - struct ceph_authorizer *a) 658 - { 659 - struct ceph_x_authorizer *au = (void *)a; 660 - 661 - ceph_x_authorizer_cleanup(au); 662 - kfree(au); 663 - } 664 - 665 645 666 646 static void ceph_x_reset(struct ceph_auth_client *ac) 667 647 { ··· 770 770 .create_authorizer = ceph_x_create_authorizer, 771 771 .update_authorizer = ceph_x_update_authorizer, 772 772 .verify_authorizer_reply = ceph_x_verify_authorizer_reply, 773 - .destroy_authorizer = ceph_x_destroy_authorizer, 774 773 .invalidate_authorizer = ceph_x_invalidate_authorizer, 775 774 .reset = ceph_x_reset, 776 775 .destroy = ceph_x_destroy,
+1
net/ceph/auth_x.h
··· 26 26 27 27 28 28 struct ceph_x_authorizer { 29 + struct ceph_authorizer base; 29 30 struct ceph_crypto_key session_key; 30 31 struct ceph_buffer *buf; 31 32 unsigned int service;
+2 -4
net/ceph/osd_client.c
··· 1087 1087 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), 1088 1088 atomic_read(&osd->o_ref) - 1); 1089 1089 if (atomic_dec_and_test(&osd->o_ref)) { 1090 - struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth; 1091 - 1092 1090 if (osd->o_auth.authorizer) 1093 - ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer); 1091 + ceph_auth_destroy_authorizer(osd->o_auth.authorizer); 1094 1092 kfree(osd); 1095 1093 } 1096 1094 } ··· 2982 2984 struct ceph_auth_handshake *auth = &o->o_auth; 2983 2985 2984 2986 if (force_new && auth->authorizer) { 2985 - ceph_auth_destroy_authorizer(ac, auth->authorizer); 2987 + ceph_auth_destroy_authorizer(auth->authorizer); 2986 2988 auth->authorizer = NULL; 2987 2989 } 2988 2990 if (!auth->authorizer) {