Bug 97245 - uri: Support more path/query attributes
Summary: uri: Support more path/query attributes
Status: RESOLVED FIXED
Alias: None
Product: p11-glue
Classification: Unclassified
Component: p11-kit (show other bugs)
Version: unspecified
Hardware: All All
: medium normal
Assignee: Stef Walter
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-08 15:09 UTC by Daiki Ueno
Modified: 2016-12-13 14:16 UTC (History)
1 user (show)

See Also:
i915 platform:
i915 features:


Attachments
uri: Remove whitespace early when parsing (11.84 KB, patch)
2016-08-08 15:09 UTC, Daiki Ueno
Details | Splinter Review
uri: Support CK_SLOT_INFO path attributes (11.47 KB, patch)
2016-08-08 15:09 UTC, Daiki Ueno
Details | Splinter Review
uri: Support 'slot-id' attribute (7.14 KB, patch)
2016-08-08 15:09 UTC, Daiki Ueno
Details | Splinter Review
uri: Support 'module-name' and 'module-path' (8.86 KB, patch)
2016-08-08 15:09 UTC, Daiki Ueno
Details | Splinter Review
uri: Remove whitespace early when parsing (10.67 KB, patch)
2016-08-11 16:00 UTC, Daiki Ueno
Details | Splinter Review
uri: Support slot info path attributes (11.32 KB, patch)
2016-08-11 16:02 UTC, Daiki Ueno
Details | Splinter Review
uri: Support 'slot-id' path attribute (7.06 KB, patch)
2016-08-11 16:03 UTC, Daiki Ueno
Details | Splinter Review
iter: Utilize slot info URI path attributes (5.96 KB, patch)
2016-08-11 16:03 UTC, Daiki Ueno
Details | Splinter Review
iter: Utilize 'slot-id' URI path attribute (4.21 KB, patch)
2016-08-11 16:03 UTC, Daiki Ueno
Details | Splinter Review
uri: Fix buffer overflow in memcmp() (7.04 KB, patch)
2016-09-20 10:21 UTC, Daiki Ueno
Details | Splinter Review
uri: Port to PKCS#11 GNU calling convention (820 bytes, patch)
2016-09-23 11:51 UTC, Daiki Ueno
Details | Splinter Review

Description Daiki Ueno 2016-08-08 15:09:04 UTC
As reported in:
https://bugzilla.redhat.com/show_bug.cgi?id=1251018

The first patch is a cleanup and the others add support for those attributes.
Comment 1 Daiki Ueno 2016-08-08 15:09:06 UTC
Created attachment 125597 [details] [review]
uri: Remove whitespace early when parsing

For every path/query component, p11_kit_uri_parse() allocates a small
buffer to strip whitespace out.  This patch removes any whitespace in
the URI at the entry of the function to simplify the code.

Note that RFC 7512 actually suggests to ignore whitespace at the
extracting phase rather than the parsing phase.
Comment 2 Daiki Ueno 2016-08-08 15:09:08 UTC
Created attachment 125598 [details] [review]
uri: Support CK_SLOT_INFO path attributes

Accept 'slot-description' and 'slot-manifacturer' path attributes
defined in RFC 7512.
Comment 3 Daiki Ueno 2016-08-08 15:09:11 UTC
Created attachment 125599 [details] [review]
uri: Support 'slot-id' attribute

Accept 'slot-id' path attribute defined in RFC 7512.
Comment 4 Daiki Ueno 2016-08-08 15:09:14 UTC
Created attachment 125600 [details] [review]
uri: Support 'module-name' and 'module-path'

Accept 'module-name' and 'module-path' query attributes defined in RFC
7512.
Comment 5 Stef Walter 2016-08-10 12:11:10 UTC
Comment on attachment 125597 [details] [review]
uri: Remove whitespace early when parsing

Review of attachment 125597 [details] [review]:
-----------------------------------------------------------------

Mostly looks good. I assume this is to make implementing the following patches easier.

This patch needs tests covering each valid input, each path, and each failure mode.

::: p11-kit/uri.c
@@ +1046,5 @@
>  
>  	if (dot == start)
>  		return P11_KIT_URI_BAD_VERSION;
> +	endptr = NULL;
> +	val = strtol (start, &endptr, 10);

Can't this cause access to memory after |end| in the case where the number is at the end of the string. It could fail when |end| is the back of a memory page, or |end| happens to be trailed by numbers until the back of a memory page.
Comment 6 Stef Walter 2016-08-10 12:15:28 UTC
Comment on attachment 125598 [details] [review]
uri: Support CK_SLOT_INFO path attributes

Review of attachment 125598 [details] [review]:
-----------------------------------------------------------------

Looks good to me.
Comment 7 Stef Walter 2016-08-10 12:16:56 UTC
Comment on attachment 125599 [details] [review]
uri: Support 'slot-id' attribute

Review of attachment 125599 [details] [review]:
-----------------------------------------------------------------

This adds two warnings:

/data/src/p11-kit/x86_64/../p11-kit/uri.c:1289:17: warning: unused variable ‘module_path’ [-Wunused-variable]
  unsigned char *module_path;
                 ^
/data/src/p11-kit/x86_64/../p11-kit/uri.c:1288:17: warning: unused variable ‘module_name’ [-Wunused-variable]

::: p11-kit/uri.c
@@ +1230,5 @@
> +	if (memcmp ("slot-id", name_start, name_end - name_start) == 0) {
> +		char *endptr;
> +		unsigned long slot_id;
> +		endptr = NULL;
> +		slot_id = strtoul (start, &endptr, 10);

Seems this can result in accessing memory out of bounds, if the number runs all the way until |end|.
Comment 8 Stef Walter 2016-08-10 12:18:30 UTC
Comment on attachment 125600 [details] [review]
uri: Support 'module-name' and 'module-path'

Review of attachment 125600 [details] [review]:
-----------------------------------------------------------------

Looks good to me.
Comment 9 Stef Walter 2016-08-10 12:19:28 UTC
In general I like this. I do have one more question though, does this affect the rest of p11-kit at all. What about iter.c, enumerate.c or list.c. Will you be updating those as a follow up to take these changes into account?
Comment 10 Daiki Ueno 2016-08-11 15:41:34 UTC
(In reply to Stef Walter from comment #9)
> In general I like this. I do have one more question though, does this affect
> the rest of p11-kit at all. What about iter.c, enumerate.c or list.c. Will
> you be updating those as a follow up to take these changes into account?

Thank you for the review.  I completely missed the use of P11KitUri in iter.c.  I will submit patches for this here as well.
Comment 11 Daiki Ueno 2016-08-11 15:45:08 UTC
Comment on attachment 125600 [details] [review]
uri: Support 'module-name' and 'module-path'

After pondering over this, I doubt that "module-name" and "module-path" are really useful in the context of p11-kit.  They are query attribute (not path attribute) which doesn't affect the matching behaviour but modify the default settings for accessing module.  As far as I understand, that task should be done by p11-kit configuration.
Comment 12 Daiki Ueno 2016-08-11 16:00:57 UTC
Created attachment 125704 [details] [review]
uri: Remove whitespace early when parsing

For every path/query component, p11_kit_uri_parse() allocates a small
buffer to strip whitespace out.  This patch removes any whitespace in
the URI at the entry of the function to simplify the code.

Note that RFC 7512 actually suggests to ignore whitespace at the
extracting phase rather than the parsing phase.
--
Restored atoin() and replaced the strtol() use with it.
Comment 13 Daiki Ueno 2016-08-11 16:02:23 UTC
Created attachment 125705 [details] [review]
uri: Support slot info path attributes

Accept 'slot-description' and 'slot-manifacturer' path attributes
defined in RFC 7512.
--
Just a summary line change, to match the subsequent patches
Comment 14 Daiki Ueno 2016-08-11 16:03:04 UTC
Created attachment 125706 [details] [review]
uri: Support 'slot-id' path attribute

Accept 'slot-id' path attribute defined in RFC 7512.
--
Use atoin() instead of strtoul().
Comment 15 Daiki Ueno 2016-08-11 16:03:14 UTC
Created attachment 125707 [details] [review]
iter: Utilize slot info URI path attributes
Comment 16 Daiki Ueno 2016-08-11 16:03:18 UTC
Created attachment 125708 [details] [review]
iter: Utilize 'slot-id' URI path attribute
Comment 17 Stef Walter 2016-09-02 15:57:49 UTC
Nice. Merged.
Comment 18 Daiki Ueno 2016-09-20 10:21:10 UTC
Created attachment 126649 [details] [review]
uri: Fix buffer overflow in memcmp()

The commit 63644dc introduced several memcmp() calls without checking
the length of the first argument.
--
I apologize that the first patch was immature and requires this fix.
Comment 19 Stef Walter 2016-09-20 12:05:14 UTC
Comment on attachment 126649 [details] [review]
uri: Fix buffer overflow in memcmp()

Review of attachment 126649 [details] [review]:
-----------------------------------------------------------------

Could you add a test that verifies this fix? A test of parsing a URL that exposed this bug?
Comment 20 Daiki Ueno 2016-09-20 12:18:48 UTC
(In reply to Stef Walter from comment #19)
> Comment on attachment 126649 [details] [review] [review]
> uri: Fix buffer overflow in memcmp()
> 
> Review of attachment 126649 [details] [review] [review]:
> -----------------------------------------------------------------
> 
> Could you add a test that verifies this fix? A test of parsing a URL that
> exposed this bug?

It was exposed by ASAN, using the existing test-uri program.
Comment 21 Daiki Ueno 2016-09-20 12:53:56 UTC
To reproduce, you could try:

 NOCONFIGURE=1 ./autogen.sh
 mkdir build
 cd build
 ../configure CFLAGS="-fsanitize=address -g -fno-common -U_FORTIFY_SOURCE" CXXFLAGS="-fsanitize=address -g -fno-common -U_FORTIFY_SOURCE" LDFLAGS="-fsanitize=address -g -fno-common -U_FORTIFY_SOURCE" LIBS="-ldl -lpthread"
 make
 ./test-uri

cf. https://mail.gnome.org/archives/desktop-devel-list/2016-September/msg00037.html
Comment 22 Daiki Ueno 2016-09-23 11:51:10 UTC
Created attachment 126743 [details] [review]
uri: Port to PKCS#11 GNU calling convention

--
One more thing I noticed while building GnuTLS with git master, sorry.
Comment 23 Daiki Ueno 2016-10-07 08:40:11 UTC
Reopening so that the additional two fixes are not forgotten.
Comment 24 Daiki Ueno 2016-12-13 14:16:45 UTC
The remaining patches were merged into git:
https://github.com/p11-glue/p11-kit/pull/5


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.