xenconsole: merge pty access check into when it is opened
PATCH 1/1 xenconsole: merge pty access check into when it is opened
This stops pty_path from being leaked, and removes the toctou race, FWIW.
Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
--- tools/console/client/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/console/client/main.c +5 -5
@@ -116,12 +116,12 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) -err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) { +if (errno !=EACCES ) +err (errno,"Could not open tty `%s'" , + pty_path); + }free (pty_path); } }
Signature · 1 line
1.7.10.4
Quoted 19 lines · Matthew Daley
This stops
pty_pathfrom being leaked, and removes the toctou race, FWIW.Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
Signed-off-by Matthew Daley <[email protected]>--- tools/console/client/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)tools/console/client/main.c +0
-0@@ -116,12 +116,12 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) {
tools/console/client/main.c static int get_pty_fd(struct xs_handle *xs, char *path, int seconds) · line 123
Quoted 10 lines
tools/console/client/main.c +4
-5-if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) -err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) { +if (errno !=EACCES ) +err (errno,"Could not open tty `%s'" , + pty_path);
access() can fail for many more reasons than just EACCES. I would skip the errno check entirely and always print the error.
~Andrew
Quoted 4 lines
tools/console/client/main.c +1
-0+ }free (pty_path); } }
Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Matthew Daley
This stops
pty_pathfrom being leaked, and removes the toctou race, FWIW.Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
Signed-off-by Matthew Daley <[email protected]>--- tools/console/client/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)tools/console/client/main.c +0
-4@@ -116,12 +116,12 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) - err(errno, "Could not open tty `%s'",
Quoted 8 lines
tools/console/client/main.c static int get_pty_fd(struct xs_handle *xs, char *path, int seconds) · line 123
Quoted 5 lines
tools/console/client/main.c +4
-1- pty_path); +if (pty_fd ==-1 ) { +if (errno !=EACCES ) +err (errno,"Could not open tty `%s'" , + pty_path);
access()can fail for many more reasons than just EACCES. I would skip the errno check entirely and always print the error.
err() doesn't return though (calls exit()). Given that, is always calling it still acceptable?
- Matthew
Quoted 34 lines · Matthew Daley
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Matthew Daley
This stops
pty_pathfrom being leaked, and removes the toctou race, FWIW.Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
Signed-off-by Matthew Daley <[email protected]>--- tools/console/client/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)tools/console/client/main.c +0
-3@@ -116,12 +116,12 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 )Quoted 8 lines
tools/console/client/main.c static int get_pty_fd(struct xs_handle *xs, char *path, int seconds) · line 123
Quoted 6 lines
tools/console/client/main.c +4
-2-err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) { +if (errno !=EACCES ) +err (errno,"Could not open tty `%s'" , + pty_path);
access()can fail for many more reasons than just EACCES. I would skip the errno check entirely and always print the error.
Quoted 4 lines
err()doesn't return though (callsexit()). Given that, is always calling it still acceptable?- Matthew
Hmm - that is a point.
Even as the patch currently stands, the behaviour of the loop has changed. Before, it would eat any error from access(), and only die in the toctou case where open() subsequently failed.
The code is waiting for an individual pty path found from xenstore. I would think that any errors resulting from a bad path being present in xenstore should probably count as fatal. After all, this is a xenconsole client asking xenconsoled which pty to connect to (which now I come to think of it, given an implicit assumption that the client is in the same domain as xenconsoled).
~Andrew
PATCH v2 1/1 xenconsole: merge pty access check into when it is opened
This stops pty_path from being leaked, and removes the toctou race, FWIW.
Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
---
v2: err() if open() fails with EACCES as well
tools/console/client/main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
tools/console/client/main.c +2 -5
@@ -116,12 +116,9 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) -err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) +err (errno,"Could not open tty `%s'" , pty_path);free (pty_path); } }
Signature · 1 line
1.7.10.4
Quoted 7 lines · Matthew Daley
This stops
pty_pathfrom being leaked, and removes the toctou race, FWIW.Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
Signed-off-by Matthew Daley <[email protected]>
Quoted 25 lines
---
v2:err()ifopen()fails with EACCES as welltools/console/client/main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)tools/console/client/main.c +2
-5@@ -116,12 +116,9 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) -err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) +err (errno,"Could not open tty `%s'" , pty_path);free (pty_path); } }
Pingy ping?
Quoted 35 lines · Matthew Daley
This stops
pty_pathfrom being leaked, and removes the toctou race, FWIW.Not sure why it's a separate check to begin with...
Coverity-ID: 1056047
Signed-off-by Matthew Daley <[email protected]>---
v2:err()ifopen()fails with EACCES as welltools/console/client/main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)tools/console/client/main.c +2
-6@@ -116,12 +116,9 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) -err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) +err (errno,"Could not open tty `%s'" , pty_path);free (pty_path); } } --1.7.10.4
Andrew Cooper writes ("Re: [Xen-devel] [PATCH] xenconsole: merge pty access check into when it is opened"):
Quoted 7 lines · Matthew Daley
Quoted 6 lines
Quoted 2 lines · Andrew Cooper
access()can fail for many more reasons than just EACCES. I would skip the errno check entirely and always print the error.
err()doesn't return though (callsexit()). Given that, is always calling it still acceptable?
...
Quoted 3 lines
Even as the patch currently stands, the behaviour of the loop has changed. Before, it would eat any error from
access(), and only die in the toctou case whereopen()subsequently failed.
I had the impression there is sometimes a race during domain startup, between a toolstack invoking xenconsole and xenconsoled allocating the pty.
Ian.
Quoted 9 lines · Ian Jackson
Andrew Cooper writes ("Re: [Xen-devel] [PATCH] xenconsole: merge pty access check into when it is opened"):
Quoted 7 lines · Matthew Daley
Quoted 6 lines
Quoted 2 lines · Andrew Cooper
access()can fail for many more reasons than just EACCES. I would skip the errno check entirely and always print the error.
err()doesn't return though (callsexit()). Given that, is always calling it still acceptable?...
Quoted 8 lines
Quoted 3 lines
Even as the patch currently stands, the behaviour of the loop has changed. Before, it would eat any error from
access(), and only die in the toctou case whereopen()subsequently failed.I had the impression there is sometimes a race during domain startup, between a toolstack invoking xenconsole and xenconsoled allocating the pty.
Ian.
In which case the xenstore path should be NULL and we wander around loop and wait on the watch again.
~Andrew
Re: [PATCH] xenconsole: merge pty access check into when it is opened [and 1 more messages]
In reply to Andrew CooperAndrew Cooper writes ("Re: [Xen-devel] [PATCH] xenconsole: merge pty access check into when it is opened"):
Quoted 3 lines
Even as the patch currently stands, the behaviour of the loop has changed. Before, it would eat any error from
access(), and only die in the toctou case whereopen()subsequently failed.
I concur, and v2 of the patch does the same. This is probably correct as Andrew and I discussed.
But the deliberate (or, at least, probably-desirable) behaviour change is not reflected in the commit message which only discusses removing the race and saving a leak.
Matthew Daley writes ("Re: [PATCH v2] xenconsole: merge pty access check into when it is opened"):
Quoted 1 line
Pingy ping?
Sorry for not making this clear sooner, but:
I think this patch is a bugfix and should go into 4.4 so I am happy to apply it but the commit message should be improved. Would you like to do that ?
Thanks,
Ian.
Re: [PATCH] xenconsole: merge pty access check into when it is opened [and 1 more messages]
Quoted 12 lines · Ian Jackson
Andrew Cooper writes ("Re: [Xen-devel] [PATCH] xenconsole: merge pty access check into when it is opened"):
Quoted 3 lines
Even as the patch currently stands, the behaviour of the loop has changed. Before, it would eat any error from
access(), and only die in the toctou case whereopen()subsequently failed.I concur, and v2 of the patch does the same. This is probably correct as Andrew and I discussed.
But the deliberate (or, at least, probably-desirable) behaviour change is not reflected in the commit message which only discusses removing the race and saving a leak.
Quoted 8 lines
Matthew Daley writes ("Re: [PATCH v2] xenconsole: merge pty access check into when it is opened"):
Quoted 1 line
Pingy ping?
Sorry for not making this clear sooner, but:
I think this patch is a bugfix and should go into 4.4 so I am happy to apply it but the commit message should be improved. Would you like to do that ?
Sure thing. v3 on its way...
- Matthew
Quoted 3 lines
Thanks,
Ian.
PATCH v3 1/1 xenconsole: adjust pty opening error checking and handling
In reply to Ian JacksonCurrently we check the pty path received from xenstore with access(); if it indicates that the pty is not accessible, we loop around and wait for a new path to appear in xenstore.
This has several issues:
* If a path has been written to xenstore, it can be assumed that that pty should already be accessible to xenconsole, and hence any error that occurs while trying to open it should be fatal and not ignored
* If access() indicates no access to the pty, the memory allocated for the path is leaked when going around the loop again
* The accessibility of the pty could change between the access() and open() calls, leading to a TOCTOU race (this is what Coverity is complaining about).
By removing the explicit access() check and just erroring out whenever open() fails, we fix all these issues.
Coverity-ID: 1056047
---
v3: Improve commit message to explain what behaviours are fixed (and hence changed)
tools/console/client/main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
tools/console/client/main.c +2 -5
@@ -116,12 +116,9 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)* disambiguate: just read the pty path */ pty_path =xs_read (xs,XBT_NULL , path, &len);if (pty_path !=NULL ) { -if (access (pty_path,R_OK |W_OK ) !=0 ) -continue ; pty_fd =open (pty_path,O_RDWR |O_NOCTTY ); -if (pty_fd ==-1 ) -err (errno,"Could not open tty `%s'" , - pty_path); +if (pty_fd ==-1 ) +err (errno,"Could not open tty `%s'" , pty_path);free (pty_path); } }
Signature · 1 line
1.7.10.4
Re: [PATCH v3] xenconsole: adjust pty opening error checking and handling
Matthew Daley writes ("[PATCH v3] xenconsole: adjust pty opening error checking and handling"):
Quoted 3 lines
Currently we check the pty path received from xenstore with
access(); if it indicates that the pty is not accessible, we loop around and wait for a new path to appear in xenstore.
Thanks, and sorry to have made such a meal of this.