Read/Write File in data folder

1,528 views
Skip to first unread message

Ben Linus

unread,
Jan 24, 2010, 11:58:04 AM1/24/10
to android-platform
Hi all,
Sorry for my English, i try to read and write a file in data folder.
I modify the class SmsManager in frameworks/base/telephony/java/
android/telephony and i add this function:

public void addNewLine(String newLine,String type){

try {

log = new File(Environment.getDataDirectory
(),"logger");

if(!log.exists())
log.createNewFile();

logTmp = File.createTempFile("logger", ".tmp",
Environment.getDataDirectory());

fis = new FileInputStream(log);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
fos = new FileOutputStream(logTmp);
ps = new PrintStream(fos);

String line = br.readLine();

while(line != null){
ps.println(line);
line = br.readLine();
}

//Line of send Message
if(type.equals("sms"))
ps.println("Invio di un messaggio al numero: " + newLine);

log.delete();
logTmp.renameTo(log);

br.close();
isr.close();
fis.close();
ps.close();
fos.close();

} catch (IOException e) {
e.printStackTrace();
}
}

I compiled the android source and when run the emulator, in the logcat
i have this error:

java.io.IOException: Parent directory of file is not writable

Why this error and what's the solution?

Thanks

Ben Linus

unread,
Jan 27, 2010, 6:30:05 AM1/27/10
to android-platform
Nothing ideas????

Dianne Hackborn

unread,
Jan 27, 2010, 1:06:49 PM1/27/10
to android-...@googlegroups.com
It looks like you are trying to write a file to /data/data, but only the system uid has permission to do that.

Also... please be extremely careful about doing whatever it is you are doing.  You are making the framework generate various files under the feet of whatever application is using it.  You could easily leave trash files around that never get cleaned up (for example if your process gets kill while this code is running).

Nothing ideas????
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Evgeny V

unread,
Jan 27, 2010, 6:43:08 AM1/27/10
to android-...@googlegroups.com
Try to add the permissions to the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


Nothing ideas????

Ben Linus

unread,
Jan 28, 2010, 4:38:47 AM1/28/10
to android-platform
Thank you Dianne for the answer,
I have already designed a "garbage collector" system for this trash
files but
I do not know how to get the system uid privileges.
I have modified PackageManagerService.java and in this class i'm able
to
create,read and write the file while in the SmsManager.java i'm not
able, why??
It is possible for a service in framework level to get the system uid
privileges?
What is the mechanism that rules this privileges?
What is the level at which my own service can obtain this privileges?

thanks in advance


On 27 Gen, 19:06, Dianne Hackborn <hack...@android.com> wrote:
> It looks like you are trying to write a file to /data/data, but only the
> system uid has permission to do that.
>
> Also... please be extremely careful about doing whatever it is you are
> doing.  You are making the framework generate various files under the feet
> of whatever application is using it.  You could easily leave trash files
> around that never get cleaned up (for example if your process gets kill
> while this code is running).
>

> > android-platfo...@googlegroups.com<android-platform%2Bunsu...@googlegroups.com>


> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-platform?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer

> hack...@android.com

Dianne Hackborn

unread,
Jan 28, 2010, 3:00:33 PM1/28/10
to android-...@googlegroups.com
On Thu, Jan 28, 2010 at 1:38 AM, Ben Linus <fabrizio...@gmail.com> wrote:
I do not know how to get the system uid privileges.

You can't.  You are modifying framework library code that runs in each application's process, under their own UID.
 
I have modified PackageManagerService.java and in this class i'm able
to
create,read and write the file while in the SmsManager.java i'm not
able, why??

The package manager service runs in the system process, as the system uid, and the system is the only thing that can modify those directories.
 
It is possible for a service in framework level to get the system uid
privileges?

Absolutely positively not.
 
What is the mechanism that rules this privileges?

 
What is the level at which my own service can obtain this privileges?

You need to have code running in the system process.

--
Dianne Hackborn
Android framework engineer
hac...@android.com

lbcoder

unread,
Jan 29, 2010, 8:05:08 AM1/29/10
to android-platform
That is all just under the assumption that he wants to be strictly
limited to working through android. There is also the option of using
native binaries setuid as system user... However, it is likely not to
go over well as a submission to AOSP unless it can be PROVEN safe and
valuable -- which is EXTREMELY difficult since setuid binaries bring
with them the risk of privilege escalation vulnerabilities.

On Jan 28, 3:00 pm, Dianne Hackborn <hack...@android.com> wrote:


> On Thu, Jan 28, 2010 at 1:38 AM, Ben Linus <fabriziomazzar...@gmail.com>wrote:
>
> > I do not know how to get the system uid privileges.
>
> You can't.  You are modifying framework library code that runs in each
> application's process, under their own UID.
>
> > I have modified PackageManagerService.java and in this class i'm able
> > to
> > create,read and write the file while in the SmsManager.java i'm not
> > able, why??
>
> The package manager service runs in the system process, as the system uid,
> and the system is the only thing that can modify those directories.
>
> > It is possible for a service in framework level to get the system uid
> > privileges?
>
> Absolutely positively not.
>
> > What is the mechanism that rules this privileges?
>

> Basic security as perhttp://developer.android.com/guide/topics/security/security.html


>
> > What is the level at which my own service can obtain this privileges?
>
> You need to have code running in the system process.
>
> --
> Dianne Hackborn
> Android framework engineer

> hack...@android.com

Dianne Hackborn

unread,
Jan 29, 2010, 9:27:32 PM1/29/10
to android-...@googlegroups.com
On Fri, Jan 29, 2010 at 5:05 AM, lbcoder <lbc...@gmail.com> wrote:
That is all just under the assumption that he wants to be strictly
limited to working through android. There is also the option of using
native binaries setuid as system user... However, it is likely not to
go over well as a submission to AOSP unless it can be PROVEN safe and
valuable -- which is EXTREMELY difficult since setuid binaries bring
with them the risk of privilege escalation vulnerabilities.

You can just assume we would not accept something like that. :)

--
Dianne Hackborn
Android framework engineer
hac...@android.com
Reply all
Reply to author
Forward
0 new messages