tools/xenstore: fix get_spec_node()
v2 cover tools/xenstore: fix get_spec_node()
Small series to fix a bug in get_spec_node().
Patch 1 is turning several function parameters into const in order to avoid having to cast away the const attribute in get_spec_node().
Patch 2 is the fix, which is a backport candidate.
Alternatives to this series would be:
- merge the patches into one patch and backport that
- swap the sequence of the patches in order to have less code churn when backporting, but re-adding the cast from const to non-cont, while backporting only the fix
- leave the series as is and backport both patches
- leave the series as is and use V1 of patch 2 for the backport
Juergen Gross (2):
tools/xenstore: add const to the return type of canonicalize() tools/xenstore: fix get_spec_node()
tools/xenstore/xenstored_core.c | 27 ++++++++++++++++----------- tools/xenstore/xenstored_core.h | 3 ++- tools/xenstore/xenstored_watch.c | 16 ++++++++++------ 3 files changed, 28 insertions(+), 18 deletions(-)
Signature · 1 line
2.35.3
PATCH v2 1/2 tools/xenstore: add const to the return type of canonicalize()
The return type of canonicalize() can be modified to const char *. This avoids the need to cast the const away from the input parameter.
There need to be quite some other functions modified to take const parameters in order to avoid further casts.
--- tools/xenstore/xenstored_core.c | 22 ++++++++++++---------- tools/xenstore/xenstored_core.h | 3 ++- tools/xenstore/xenstored_watch.c | 16 ++++++++++------ 3 files changed, 24 insertions(+), 17 deletions(-)
tools/xenstore/xenstored_core.c +12 -10
@@ -1216,25 +1216,26 @@ static char *perms_to_strings(const void *ctx, const struct node_perms *perms,return strings; } -char *canonicalize (struct connection *conn,const void *ctx,const char *node) +const char *canonicalize (struct connection *conn,const void *ctx, +const char *node) {const char *prefix;if (!node || (node[0 ] =='/' ) || (node[0 ] =='@' )) -return (char *)node; +return node; prefix =get_implicit_path (conn);if (prefix)return talloc_asprintf (ctx,"%s/%s" , prefix, node); -return (char *)node; +return node; }static struct node *get_node_canonicalized (struct connection *conn,const void *ctx,const char *name, -char **canonical_name, +const char **canonical_name,unsigned int perm) { -char *tmp_name; +const char *tmp_name;if (!canonical_name) canonical_name = &tmp_name;
@@ -1249,7 +1250,7 @@ static struct node *get_node_canonicalized(struct connection *conn, }static struct node *get_spec_node (struct connection *conn,const void *ctx, -const char *name,char **canonical_name, +const char *name,const char **canonical_name,unsigned int perm) {if (name[0 ] =='@' )
@@ -1539,7 +1540,7 @@ static int do_write(const void *ctx, struct connection *conn,unsigned int offset, datalen;struct node *node;char *vec[1 ] = {NULL };/* gcc4 + -W + -Werror fucks code. */ -char *name; +const char *name;/* Extra "strings" can be created by binary data. */ if (get_strings (in, vec,ARRAY_SIZE (vec)) <ARRAY_SIZE (vec))
@@ -1574,7 +1575,7 @@ static int do_mkdir(const void *ctx, struct connection *conn, struct buffered_data *in) {struct node *node; -char *name; +const char *name; node =get_node_canonicalized (conn, ctx,onearg (in), &name,XS_PERM_WRITE );
@@ -1703,7 +1704,7 @@ static int do_rm(const void *ctx, struct connection *conn, {struct node *node;int ret; -char *name; +const char *name;char *parentname; node = get_node_canonicalized(conn, ctx, onearg(in), &name,
@@ -1765,7 +1766,8 @@ static int do_set_perms(const void *ctx, struct connection *conn, struct buffered_data *in) {struct node_perms perms, old_perms; -char *name, *permstr; +const char *name; +char *permstr;struct node *node; perms.num =xenstore_count_strings (in->buffer, in->used);
tools/xenstore/xenstored_core.h +2 -1
@@ -224,7 +224,8 @@ void send_event(struct buffered_data *req, struct connection *conn,void send_ack (struct connection *conn,enum xsd_sockmsg_type type);/* Canonicalize this path if possible. */ -char *canonicalize (struct connection *conn,const void *ctx,const char *node); +const char *canonicalize (struct connection *conn,const void *ctx, +const char *node);/* Get access permissions. */ unsigned int perm_for_conn(struct connection *conn,
tools/xenstore/xenstored_watch.c +10 -6
@@ -161,7 +161,7 @@ static int destroy_watch(void *_watch) }static int check_watch_path (struct connection *conn,const void *ctx, -char **path,bool *relative) +const char **path,bool *relative) {/* Check if valid event. */ if (strstarts (*path,"@" )) {
@@ -184,8 +184,9 @@ static int check_watch_path(struct connection *conn, const void *ctx,return errno; } -static struct watch *add_watch (struct connection *conn,char *path,char *token, -bool relative,bool no_quota_check) +static struct watch *add_watch (struct connection *conn,const char *path, +const char *token,bool relative, +bool no_quota_check) {struct watch *watch;
@@ -218,12 +219,14 @@ int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in) {struct watch *watch;char *vec[2 ]; +const char *path;bool relative;if (get_strings (in, vec,ARRAY_SIZE (vec)) !=ARRAY_SIZE (vec))return EINVAL ; - errno =check_watch_path (conn, ctx, &(vec[0 ]), &relative); + path = vec[0 ]; + errno =check_watch_path (conn, ctx, &path, &relative);if (errno)return errno;
@@ -258,7 +261,8 @@ int do_unwatch(const void *ctx, struct connection *conn, struct buffered_data *in) {struct watch *watch; -char *node, *vec[2 ]; +const char *node; +char *vec[2 ];if (get_strings (in, vec,ARRAY_SIZE (vec)) !=ARRAY_SIZE (vec))return EINVAL ;
@@ -336,7 +340,7 @@ void read_state_watch(const void *ctx, const void *state) {const struct xs_state_watch *sw = state;struct connection *conn; -char *path, *token; +const char *path, *token;bool relative; conn =get_connection_by_id (sw->conn_id);
Signature · 1 line
2.35.3
Re: [PATCH v2 1/2] tools/xenstore: add const to the return type of canonicalize()
Hi Juergen,
Quoted 11 lines · Juergen Gross
tools/xenstore/xenstored_watch.c +3
-2-static struct watch *add_watch (struct connection *conn,char *path,char *token, -bool relative,bool no_quota_check) +static struct watch *add_watch (struct connection *conn,const char *path, +const char *token,bool relative, +bool no_quota_check) {struct watch *watch;@@ -218,12 +219,14 @@ int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in) {struct watch *watch;
tools/xenstore/xenstored_watch.c int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in) · line 229
Quoted 10 lines
tools/xenstore/xenstored_watch.c +3
-1char *vec[2 ]; +const char *path;bool relative;if (get_strings (in, vec,ARRAY_SIZE (vec)) !=ARRAY_SIZE (vec))return EINVAL ; - errno =check_watch_path (conn, ctx, &(vec[0 ]), &relative); + path = vec[0 ]; + errno =check_watch_path (conn, ctx, &path, &relative);
From my understanding, check_watch_path() could update vec[0] which is then used below. But with your change, vec[0] would not be updated anymore.
I can see two possible approach:
1) get_strings() is taking a const as well
2) replace all use of vec[0] with path
I have a slight preference for the former. But I can also understand this is more work. So choose whichever you prefer.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 1/2] tools/xenstore: add const to the return type of canonicalize()
Quoted 28 lines · Julien Grall
Hi Juergen,
Quoted 11 lines · Juergen Gross
-static struct watch *add_watch(struct connection *conn, char *path, char *token,
- bool relative, boolno_quota_check) +static struct watch *add_watch(struct connection *conn, const char *path,
+ const char *token, bool relative,
+ boolno_quota_check)
{
struct watch *watch;tools/xenstore/xenstored_watch.c +0
-0@@ -218,12 +219,14 @@ int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in)tools/xenstore/xenstored_watch.c int do_watch(const void *ctx, struct connection · line 228
Quoted 10 lines
tools/xenstore/xenstored_watch.c +3
-1{ struct watch *watch; char *vec[2]; + const char *path; bool relative; if (get_strings(in, vec, ARRAY_SIZE(vec)) != ARRAY_SIZE(vec)) return EINVAL; - errno = check_watch_path(conn, ctx, &(vec[0]), &relative); + path = vec[0]; + errno = check_watch_path(conn, ctx, &path, &relative);From my understanding,
check_watch_path()could update vec[0] which is
then used below. But with your change, vec[0] would not be updated anymore.
Quoted 2 lines
I can see two possible approach:
1)get_strings()is taking a const as well
I have a patch doing this. I will send it on Monday.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 1/2] tools/xenstore: add const to the return type of canonicalize()
Quoted 29 lines · Julien Grall
Quoted 26 lines · Julien Grall
Hi Juergen,
Quoted 10 lines · Juergen Gross
-static struct watch *add_watch(struct connection *conn, char *path, char *token,
- bool relative, boolno_quota_check) +static struct watch *add_watch(struct connection *conn, const char *path, + const char *token, bool relative,
+ boolno_quota_check)
{
struct watch *watch;tools/xenstore/xenstored_watch.c +0
-0@@ -218,12 +219,14 @@ int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in)tools/xenstore/xenstored_watch.c int do_watch(const void *ctx, struct connection *conn, · line 228
Quoted 10 lines
tools/xenstore/xenstored_watch.c +3
-1{ struct watch *watch; char *vec[2]; + const char *path; bool relative; if (get_strings(in, vec, ARRAY_SIZE(vec)) != ARRAY_SIZE(vec)) return EINVAL; - errno = check_watch_path(conn, ctx, &(vec[0]), &relative); + path = vec[0]; + errno = check_watch_path(conn, ctx, &path, &relative);From my understanding,
check_watch_path()could update vec[0] which is then used below. But with your change, vec[0] would not be updated anymore.
Quoted 4 lines
Quoted 2 lines
I can see two possible approach:
1)get_strings()is taking a const as wellI have a patch doing this. I will send it on Monday.
Hmm, looking at this I think this will collide with my patch.
Would you mind me doing the conversion of get_strings() in my patch, too?
Juergen
Re: [PATCH v2 1/2] tools/xenstore: add const to the return type of canonicalize()
Hi,
Quoted 31 lines · Juergen Gross
Quoted 30 lines · Julien Grall
Quoted 27 lines · Julien Grall
Hi Juergen,
Quoted 11 lines · Juergen Gross
-static struct watch *add_watch(struct connection *conn, char *path, char *token,
- bool relative, boolno_quota_check) +static struct watch *add_watch(struct connection *conn, const char *path,
+ const char *token, bool relative,
+ boolno_quota_check)
{
struct watch *watch;tools/xenstore/xenstored_watch.c +0
-0@@ -218,12 +219,14 @@ int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in)tools/xenstore/xenstored_watch.c int do_watch(const void *ctx, struct · line 228
Quoted 10 lines
tools/xenstore/xenstored_watch.c +3
-1{ struct watch *watch; char *vec[2]; + const char *path; bool relative; if (get_strings(in, vec, ARRAY_SIZE(vec)) != ARRAY_SIZE(vec)) return EINVAL; - errno = check_watch_path(conn, ctx, &(vec[0]), &relative); + path = vec[0]; + errno = check_watch_path(conn, ctx, &path, &relative);From my understanding,
check_watch_path()could update vec[0] which is then used below. But with your change, vec[0] would not be updated anymore.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I can see two possible approach:
1)get_strings()is taking a const as wellI have a patch doing this. I will send it on Monday.
Hmm, looking at this I think this will collide with my patch.
Yes, it will for check_watch_patch().
Quoted 2 lines
Would you mind me doing the conversion of
get_strings()in my patch, too?
Sure.
Cheers,
Signature · 1 line
Julien Grall
PATCH v2 2/2 tools/xenstore: fix get_spec_node()
In reply to Juergen GrossIn case get_spec_node() is being called for a special node starting with '@' it won't set *canonical_name. This can result in a crash of xenstored due to dereferencing the uninitialized name in fire_watches().
This is no security issue as it requires either a privileged caller or ownership of the special node in question by an unprivileged caller (which is questionable, as this would make the owner privileged in some way).
--- tools/xenstore/xenstored_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/xenstore/xenstored_core.c +4 -1
@@ -1253,8 +1253,11 @@ static struct node *get_spec_node(struct connection *conn, const void *ctx, const char *name, const char **canonical_name, unsigned int perm) { - if (name[0] == '@') + if (name[0] == '@') { + if (canonical_name) + *canonical_name = name;return get_node (conn, ctx, name, perm); + }return get_node_canonicalized (conn, ctx, name, canonical_name, perm); }
Signature · 1 line
2.35.3
Hi Juergen,
Quoted 5 lines · Juergen Gross
In case
get_spec_node()is being called for a special node starting with '@' it won't set*canonical_name. This can result in a crash of xenstored due to dereferencing the uninitialized name infire_watches().
Quoted 7 lines
This is no security issue as it requires either a privileged caller or ownership of the special node in question by an unprivileged caller (which is questionable, as this would make the owner privileged in some way).
Fixes d6bb63924fc2 ("tools/xenstore: introduce dummy nodes for special watch paths")Signed-off-by Juergen Gross <[email protected]>
Cheers,
Signature · 1 line
Julien Grall
Hi Juergen,
Quoted 10 lines · Juergen Gross
Small series to fix a bug in
get_spec_node().Patch 1 is turning several function parameters into const in order to avoid having to cast away the const attribute in
get_spec_node().Patch 2 is the fix, which is a backport candidate.
Alternatives to this series would be:
- merge the patches into one patch and backport that
AFAIU, this would have the same outcome as applying the two patches but it would circumvent the "we don't backport clean-up to stable tree" and lose the history.
Quoted 3 lines
- swap the sequence of the patches in order to have less code churn when backporting, but re-adding the cast from const to non-cont, while backporting only the fix
I am not sure I understand this. If you swap the patch, wouldn't the const-away cast be needed to compile and allow bisection?
Quoted 1 line
- leave the series as is and backport both patches
I am split. On one hand, it would be good to harden older Xenstored, on the other hand this is not strictly necessary to fix it. So possibly not a good option compare to the others.
Quoted 1 line
- leave the series as is and use V1 of patch 2 for the backpor
It would be my preference. But I would also be happy with swapping patch #1 and patch #2 if there is a desire to have a clean cherry-pick.
Cheers,
Signature · 1 line
Julien Grall
Quoted 8 lines · Julien Grall
Hi Juergen,
Quoted 5 lines · Juergen Gross
Small series to fix a bug in
get_spec_node().Patch 1 is turning several function parameters into const in order to avoid having to cast away the const attribute in
get_spec_node().
Quoted 7 lines
Quoted 5 lines
Patch 2 is the fix, which is a backport candidate.
Alternatives to this series would be:
- merge the patches into one patch and backport that
AFAIU, this would have the same outcome as applying the two patches but it would circumvent the "we don't backport clean-up to stable tree" and lose the history.
Correct. With your suggested patch modifying get_strings() this would grow even larger, though.
Quoted 6 lines
Quoted 4 lines
- swap the sequence of the patches in order to have less code churn when backporting, but re-adding the cast from const to non-cont, while backporting only the fix
I am not sure I understand this. If you swap the patch, wouldn't the const-away cast be needed to compile and allow bisection?
Yes. This suggestion would be one way to use the const-away cast for the backports. It would basically enable us to have the same (or very similar) patch in unstable and the stable branches.
Quoted 4 lines
Quoted 2 lines
- leave the series as is and backport both patches
I am split. On one hand, it would be good to harden older Xenstored, on the other hand this is not strictly necessary to fix it. So possibly not a good option compare to the others.
I agree.
Quoted 3 lines
Quoted 2 lines
- leave the series as is and use V1 of patch 2 for the backpor
It would be my preference. But I would also be happy with swapping patch #1 and patch #2 if there is a desire to have a clean cherry-pick.
I'm fine both ways.
Juergen