libceph: use RCU to protect monmap in ceph_compare_options()
PATCH 1/1 libceph: use RCU to protect monmap in ceph_compare_options()
In reply to a message not in the archiveFrom: Yong Wang <[email protected]>
ceph_compare_options() checks whether a new mount shares any monitor address with an existing client by walking client->monc.monmap via ceph_monmap_contains(). That comparison can run under sb_lock or rbd_client_list_lock, so it cannot take monc->mutex.
Meanwhile, monmap update handling replaces monc->monmap under monc->mutex and frees the old map immediately. A concurrent shared- mount comparison can therefore dereference a freed monmap and walk stale mon_inst[] entries, triggering a use-after-free.
Protect the compare path with RCU and publish/free monitor maps with rcu_assign_pointer() and kfree_rcu(). Annotate monc->monmap as an RCU pointer and use rcu_dereference_protected() in mutex-protected paths to keep the accesses consistent with the new pointer contract.
This keeps the existing non-blocking comparison semantics while ensuring that replaced monmaps remain alive until readers are done.
--- include/linux/ceph/mon_client.h | 4 +++- net/ceph/ceph_common.c | 16 ++++++++++++---- net/ceph/mon_client.c | 34 ++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 16 deletions(-)
include/linux/ceph/mon_client.h +3 -1
@@ -4,6 +4,7 @@#include <linux/completion.h> #include <linux/kref.h> +#include <linux/rcupdate.h> #include <linux/rbtree.h> #include <linux/ceph/messenger.h>
@@ -19,6 +20,7 @@ struct ceph_monmap {struct ceph_fsid fsid;u32 epoch;u32 num_mon; +struct rcu_head rcu; struct ceph_entity_inst mon_inst[] __counted_by(num_mon); };
@@ -69,7 +71,7 @@ struct ceph_mon_generic_request {struct ceph_mon_client {struct ceph_client *client; -struct ceph_monmap *monmap; +struct ceph_monmap __rcu *monmap;struct mutex mutex;struct delayed_work delayed_work;
net/ceph/ceph_common.c +12 -4
@@ -133,6 +133,7 @@ int ceph_compare_options(struct ceph_options *new_opt, {struct ceph_options *opt1 = new_opt;struct ceph_options *opt2 = client->options; +struct ceph_monmap *monmap;int ofs = offsetof(struct ceph_options , mon_addr);int i;int ret;
@@ -180,13 +181,20 @@ int ceph_compare_options(struct ceph_options *new_opt,if (ret)return ret; +rcu_read_lock (); + monmap =rcu_dereference (client->monc.monmap); + ret =-1 ; +/* any matching mon ip implies a match */ for (i =0 ; i < opt1->num_mon; i++) { -if (ceph_monmap_contains (client->monc.monmap, - &opt1->mon_addr[i])) -return 0 ; +if (ceph_monmap_contains (monmap, &opt1->mon_addr[i])) { + ret =0 ; +break ; + } } -return -1 ; + +rcu_read_unlock (); +return ret; }EXPORT_SYMBOL (ceph_compare_options);
net/ceph/mon_client.c +23 -11
@@ -206,19 +206,22 @@ static void __close_session(struct ceph_mon_client *monc)*/ static void pick_new_mon (struct ceph_mon_client *monc) { +struct ceph_monmap *monmap = +rcu_dereference_protected (monc->monmap, +lockdep_is_held (&monc->mutex));int old_mon = monc->cur_mon; -BUG_ON (monc->monmap->num_mon <1 ); +BUG_ON (monmap->num_mon <1 ); -if (monc->monmap->num_mon ==1 ) { +if (monmap->num_mon ==1 ) { monc->cur_mon =0 ; }else { -int max = monc->monmap->num_mon; +int max = monmap->num_mon;int o =-1 ;int n;if (monc->cur_mon >=0 ) { -if (monc->cur_mon < monc->monmap->num_mon) +if (monc->cur_mon < monmap->num_mon) o = monc->cur_mon;if (o >=0 ) max--;
@@ -232,7 +235,7 @@ static void pick_new_mon(struct ceph_mon_client *monc) }dout ("%s mon%d -> mon%d out of %d mons\n" , __func__, old_mon, - monc->cur_mon, monc->monmap->num_mon); + monc->cur_mon, monmap->num_mon); }/*
@@ -240,6 +243,9 @@ static void pick_new_mon(struct ceph_mon_client *monc)*/ static void __open_session (struct ceph_mon_client *monc) { +struct ceph_monmap *monmap = +rcu_dereference_protected (monc->monmap, +lockdep_is_held (&monc->mutex));int ret;pick_new_mon (monc);
@@ -256,7 +262,7 @@ static void __open_session(struct ceph_mon_client *monc)dout ("%s opening mon%d\n" , __func__, monc->cur_mon);ceph_con_open (&monc->con,CEPH_ENTITY_TYPE_MON , monc->cur_mon, - &monc->monmap->mon_inst[monc->cur_mon].addr); + &monmap->mon_inst[monc->cur_mon].addr);/* * Queue a keepalive to ensure that in case of an early fault
@@ -542,6 +548,7 @@ static void ceph_monc_handle_map(struct ceph_mon_client *monc, struct ceph_msg *msg) {struct ceph_client *client = monc->client; +struct ceph_monmap *old_monmap;struct ceph_monmap *monmap;void *p, *end;
@@ -564,10 +571,12 @@ static void ceph_monc_handle_map(struct ceph_mon_client *monc, goto out; } -kfree (monc->monmap); - monc->monmap = monmap; + old_monmap =rcu_dereference_protected (monc->monmap, +lockdep_is_held (&monc->mutex)); +rcu_assign_pointer (monc->monmap, monmap); +kfree_rcu (old_monmap, rcu); -__ceph_monc_got_map (monc,CEPH_SUB_MONMAP , monc->monmap->epoch); +__ceph_monc_got_map (monc,CEPH_SUB_MONMAP , monmap->epoch); client->have_fsid = true; out:
@@ -775,6 +784,7 @@ static void handle_statfs_reply(struct ceph_mon_client *monc,int ceph_monc_do_statfs (struct ceph_mon_client *monc,u64 data_pool,struct ceph_statfs *buf) { +struct ceph_monmap *monmap;struct ceph_mon_generic_request *req;struct ceph_mon_statfs *h;int ret = -ENOMEM ;
@@ -802,7 +812,9 @@ int ceph_monc_do_statfs(struct ceph_mon_client *monc, u64 data_pool, h->monhdr.have_version =0 ; h->monhdr.session_mon =cpu_to_le16 (-1 ); h->monhdr.session_mon_tid =0 ; - h->fsid = monc->monmap->fsid; + monmap =rcu_dereference_protected (monc->monmap, +lockdep_is_held (&monc->mutex)); + h->fsid = monmap->fsid; h->contains_data_pool = (data_pool !=CEPH_NOPOOL ); h->data_pool =cpu_to_le64 (data_pool);send_generic_request (monc, req);
@@ -1267,7 +1279,7 @@ void ceph_monc_stop(struct ceph_mon_client *monc)ceph_msg_put (monc->m_subscribe);ceph_msg_put (monc->m_subscribe_ack); -kfree (monc->monmap); +kfree_rcu (monc->monmap, rcu); }EXPORT_SYMBOL (ceph_monc_stop);
Signature · 1 line
2.53.0
Alex Markuze
maintainerHi Ren,
NACK for now. Thanks for the patch, but I have some concerns that need a v2:
1. [major] net/ceph/mon_client.c:1032: Raw __rcu dereference in do_mon_command()
`h->fsid = monc->monmap->fsid;` is a raw dereference of the
now-`__rcu` pointer. The identical pattern in `ceph_monc_do_statfs`
was converted but this one was missed. Safe (under `monc->mutex`),
but sparse will warn and the conversion is inconsistent.
Suggested fix: Use `rcu_dereference_protected(monc->monmap,
lockdep_is_held(&monc->mutex))`, same as the other mutex-protected
sites. 2. [major] net/ceph/ceph_common.c:806: Raw __rcu dereference in wait_for_setup path have_monmap = client->monc.monmap && client->monc.monmap->epoch;
— two raw accesses to the __rcu pointer. Under monc->mutex, so
safe, but sparse will flag it.
Suggested fix: Same rcu_dereference_protected() pattern.
3. [minor] net/ceph/mon_client.c:1175: Init-path assignment to __rcu pointer without RCU_INIT_POINTER
4. [nit] commit message: Non-standard Assisted-by trailer tag
RCU approach is correct but __rcu annotation conversion is incomplete — at least two mutex-protected sites still dereference raw, which will produce sparse warnings.
Signature · 1 line
Alex Markuze