After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 710163 - canonicalize_file_name -> realpath
canonicalize_file_name -> realpath
Status: RESOLVED FIXED
Product: librsvg
Classification: Core
Component: general
2.40.x
Other OpenBSD
: Normal normal
: ---
Assigned To: Federico Mena Quintero
librsvg maintainers
: 708645 719742 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2013-10-15 07:39 UTC by Antoine Jacoutot
Modified: 2015-03-25 00:16 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
canonicalize_file_name -> realpath (1.22 KB, patch)
2013-10-15 07:39 UTC, Antoine Jacoutot
committed Details | Review
Simply avoid canonicalizing file names. (1.84 KB, patch)
2013-11-02 17:16 UTC, Mike Henning (drawoc)
none Details | Review
Use _fullpath() on Windows for realpath() (925 bytes, patch)
2014-08-18 04:33 UTC, Fan, Chun-wei
none Details | Review
W32: Use _wfullpath+g_convert to get UTF-8 support (1.20 KB, patch)
2015-03-21 19:44 UTC, LRN
none Details | Review

Description Antoine Jacoutot 2013-10-15 07:39:57 UTC
Created attachment 257324 [details] [review]
canonicalize_file_name -> realpath

Hi.

Recent version of librsvg do not build on !linux anymore because of canonicalize_file_name() which is a GNU extension and is not available everywhere.
I am not familiar with canonicalize_file_name() so maybe there was a reason I am not aware of not to use realpath() ?

Anyway, here's a patch that s/canonicalize_file_name/realpath and allows me to build librsvg 2.40.0 on OpenBSD.
Comment 1 Christian Persch 2013-10-15 10:16:04 UTC
*** Bug 708645 has been marked as a duplicate of this bug. ***
Comment 2 Christian Persch 2013-10-15 10:24:42 UTC
Using realpath with NULL isn't portable either, afaik.
Comment 3 Ting-Wei Lan 2013-10-15 10:44:51 UTC
realpath with NULL is defined in POSIX.1-2008.
Comment 4 Antoine Jacoutot 2013-10-15 11:04:18 UTC
(In reply to comment #2)
> Using realpath with NULL isn't portable either, afaik.

It may be but it is at least more portable than canonicalize_file_name which is only available on Linux imho.
Comment 5 Antoine Jacoutot 2013-10-31 10:41:52 UTC
Hi Christian.

Any final input on this? Would you prefer a patch with check for canonicalize_file_name()?
Thank you.
Comment 6 Christian Persch 2013-10-31 12:03:33 UTC
In the past I've had at least one bug report where realpath with NULL didn't work.

An alternative solution would be to use gnulib which has a module for this function.
Comment 7 Antoine Jacoutot 2013-10-31 12:20:15 UTC
(In reply to comment #6)
> An alternative solution would be to use gnulib which has a module for this
> function.

Bleh... I never used gnulib before. Is it supposed to be bundled within a project or does it needs to be installed separately then looked for?
Comment 8 Mike Henning (drawoc) 2013-11-02 17:16:57 UTC
Created attachment 258810 [details] [review]
Simply avoid canonicalizing file names.

This patch reimplements part of _rsvg_handle_allow_load, simply avoiding the calls to canonicalize_file_name by using g_file_has_prefix to do the comparison. This takes care of every use of canonicalize_file_name in the code.

My code should be equivalent, but I haven't actually tested it except for the fact that it compiles.

I think this is probably a better approach than trying to use a canonicalize_file_name substitute.
Comment 9 Antoine Jacoutot 2013-11-03 07:40:00 UTC
That seems to work for me :-)
Comment 10 Antoine Jacoutot 2013-11-14 10:26:36 UTC
Christian any comment on that approach? It would be nice to have this fixed once and for all. Thank you.
Comment 11 Christian Persch 2013-12-08 18:39:31 UTC
*** Bug 719742 has been marked as a duplicate of this bug. ***
Comment 12 Allison Karlitskaya (desrt) 2013-12-08 18:43:29 UTC
Thanks!
Comment 13 Mike Henning (drawoc) 2013-12-08 21:21:00 UTC
The patch that was committed is attachment 257324 [details] [review].

As discussed above, the function realpath with null passed in as the second argument isn't portable either. iirc, it will segfault on solaris. Additionally, the function realpath doesn't exist at all on windows.

I'm therefore reopening this, as the underlying bug (lack of portability) still has not been fixed.
Comment 14 cee1 2013-12-09 00:56:22 UTC
(In reply to comment #13)
> The patch that was committed is attachment 257324 [details] [review].
> 
attachment 263366 [details] [review]
It will validate the behavior of realpath in configure.ac, and fallback to canonicalize_file_name if the former fails and canonicalize_file_name is available on the platform, otherwise throw a compiling error.
Comment 15 Mike Henning (drawoc) 2013-12-09 15:00:34 UTC
I think that canonicalize_file_name is only really available on platforms where realpath(path, NULL) also works. So, that patch wouldn't really gain anything in terms of portability.
Comment 16 cee1 2013-12-09 15:26:19 UTC
(In reply to comment #15)
> I think that canonicalize_file_name is only really available on platforms where
> realpath(path, NULL) also works.
canonicalize_file_name is not available on Mac OS X, where realpath(path, NULL) is there.

And realpath(path, NULL) is specified in POSIX.1-2008: http://man7.org/linux/man-pages/man3/realpath.3.html
Comment 17 Mike Henning (drawoc) 2013-12-09 15:39:20 UTC
I know. What I'm saying is that there's never a reason to use canonicalize_file_name.

canonicalize_file_name only seems to exist on platforms where realpath(path, NULL) works correctly.
Comment 18 cee1 2013-12-10 11:53:29 UTC
Well, on a platform that doesn't support canonicalize_file_name, old version of librsvg will throw a compiling error.

On a platform that has realpath but not support realpath(path, NULL), current librsvg will not throw a compiling error, leave the user to debug SEGV or other errors.

So checking the behavior of realpath(path, NULL) in configure shall make sense, though it is not possible in cross-compiling case. In cross-compiling, we can use canonicalize_file_name or throw a compiling error(if no canonicalize_file_name).
Comment 19 Christian Persch 2013-12-10 12:10:22 UTC
(In reply to comment #13)
> As discussed above, the function realpath with null passed in as the second
> argument isn't portable either. iirc, it will segfault on solaris.

Have you checked that this is still true on current illumos?
Comment 20 Mike Henning (drawoc) 2013-12-10 21:42:48 UTC
No, to be honest I don't know much about the solaris/illumos case, I just remember someone mentioning that it segfaulted.

I do know that if you want to support realpath on windows, you need to add code like this (which will work with the null case):

#ifdef G_OS_WIN32
#define realpath(a,b) _fullpath(b,a,_MAX_PATH)
#endif

Although, I still haven't heard anyone review my patch (attachment 258810 [details] [review]). It seems simpler to me than using realpath. Is there any reason you refuse to use it?
Comment 21 Eduard Braun 2014-03-29 15:10:47 UTC
I ran into this issue when compiling for Windows/MinGW.

Would be great if a solution can be found. Attachment 258810 [details] seems to work on Windows. If there are no security considerations that might be a workable solution?
Comment 22 Paolo Borelli 2014-05-28 20:45:00 UTC
Review of attachment 258810 [details] [review]:

I have also run into this issue and the proposed patch makes sense to me.

However I suggest calling g_file_new_from_uri near the beginning of the function and then calling g_file_get_uri_scheme instead of g_uri_parse_scheme, this way we avoid parsing the uri twice
Comment 23 Paolo Borelli 2014-05-28 21:13:23 UTC
Actually after discussing this on irc, realpath does i/o to resolve symlinks, while working with gfile does not. So if the attacker symlinks /a/b -> /c then the gfile prefix check for /a/c has-prefix /a will succeed while the realpah won't.

We either need to implement a realpath equivalent using gio (if there isn't one already) or we need to go with one of the other solutions like that of comment #20 (if it works)
Comment 24 Fan, Chun-wei 2014-08-18 04:33:40 UTC
Created attachment 283702 [details] [review]
Use _fullpath() on Windows for realpath()

Hi,

As the upcoming GTK+-3.14 release series will need to use librsvg in some way at run time, either directly via a GDK-Pixbuf rsvg loader or indirectly via the recently-added gtk-encode-symbolic-svg, I think it would be fantastic if we can have librsvg-2.40.2+ working for Windows.  I am attaching a patch for this in accordance to Mike's suggestions in comment #20, for references.

With blessings, thanks a bunch!
Comment 25 Mike Henning (drawoc) 2014-08-18 15:23:12 UTC
I tested the patch in comment 24, and I can confirm that it fixes compilation for windows.
Comment 26 Richard Hughes 2014-12-03 09:39:04 UTC
I've pushed Chun-wei's patch to git master. Thanks!
Comment 27 LRN 2015-03-21 19:44:51 UTC
Created attachment 300037 [details] [review]
W32: Use _wfullpath+g_convert to get UTF-8 support
Comment 28 Federico Mena Quintero 2015-03-25 00:15:47 UTC
(In reply to LRN from comment #27)
> Created attachment 300037 [details] [review] [review]
> W32: Use _wfullpath+g_convert to get UTF-8 support

Thanks!  I committed the following variation, as all of Glib uses g_utf8_to_utf16() for this kind of G_OS_WIN32 special case.

+static char *
+rsvg_realpath_utf8 (const char *filename, const char *unused)
+{
+    wchar_t *wfilename;
+    wchar_t *wfull;
+    char *full;
+
+    wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
+    if (!wfilename)
+        return NULL;
+
+    wfull = _wfullpath (NULL, wfilename, 0);
+    g_free (wfilename);
+    if (!wfull)
+        return NULL;
+
+    full = g_utf16_to_utf8 (wfull, -1, NULL, NULL, NULL);
+    free (wfull);
+
+    if (!full)
+        return NULL;
+
+    return full;
+}
+
+#define realpath(a,b) rsvg_realpath_utf8 (a, b)

This is in commit 28694c791067f0321ae21d44734b1ef88b45d742