Bug 3501 - File.GetAttributes() returns ReadOnly for writable files
Summary: File.GetAttributes() returns ReadOnly for writable files
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: io-layer ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-02-16 13:58 UTC by Jonathan Pryor
Modified: 2014-01-13 00:59 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jonathan Pryor 2012-02-16 13:58:17 UTC
Context: http://mono-for-android.1047100.n5.nabble.com/System-IO-File-GetAttributes-returns-always-ReadOnly-td5430132.html

The problem is that mono/mono/io-layer/io.c!_wapi_stat_to_file_attributes() ignores st_uid and st_gid (and anything else that would be useful). Result: it _only_ checks S_IWUSR to determine if a file writable:

> if (!(buf->st_mode & S_IWUSR)) {
> 	attrs = FILE_ATTRIBUTE_READONLY;

So, consider a file not owned by the current user, but accessible by the current user because of groups:

> $ whoami
> jon
> $ groups
> staff everyone _appstore localaccounts _appserverusr admin _appserveradm _lpadmin _lpoperator _developer com.apple.access_ssh com.apple.access_screensharing
> 
> $ touch foo
> $ sudo chown root:staff foo
> $ sudo chmod 075 foo
> $  ls -l foo
> ----rwxr-x  1 root  staff  0 Feb 16 13:43 foo

The file is still writable by the current user:

> $ echo foo > foo
> # Note: no error

Mono disagrees:

> $ csharp
> Mono C# Shell, type "help;" for help
> 
> Enter statements below.
> csharp> System.IO.File.GetAttributes("foo");     
> ReadOnly

despite the fact that we can, in fact, write to it:

> csharp> using (var o = System.IO.File.AppendText("foo")) o.WriteLine("bar");
> csharp> ^D
> 
> $ cat foo
> foo
> bar

_wapi_stat_to_file_attributes() should compare st_uid to geteuid(2) (and getuid(2)?), and when they don't match check for group ownership (getgrent(3) & co.), and if group checking fails then use S_IWOTH.
Comment 1 Rodrigo Kumpera 2014-01-13 00:31:17 UTC
This is anything but trivial.

We can check the euid and egid against the supplied struct stat. But, beyond that, we have two options:

1)The getgrgid / getpwuid dance, which does work on OSX/iOS, is very complex and fail flat in face of extended permission systems like SELinux or OSX sandboxes.

2)Use access(2) and accept that it will fail with setuid/setgid programs.
Comment 2 Rodrigo Kumpera 2014-01-13 00:59:54 UTC
Fixed in master using option 2.