2011年5月23日月曜日

How to Share User IDs and File Access for Android App

Android is a privilege-separated operating system, in which each application runs with a distinct system identity (Linux user ID and group ID). Parts of the system are also separated into distinct identities. Linux thereby isolates applications from each other and from the system.

Additional finer-grained security features are provided through a "permission" mechanism that enforces restrictions on the specific operations that a particular process can perform, and per-URI permissions for granting ad-hoc access to specific pieces of data.

At install time, Android gives each package a distinct Linux user ID. The identity remains constant for the duration of the package's life on that device. On a different device, the same package may have a different UID; what matters is that each package has a distinct UID on a given device.

Because security enforcement happens at the process level, the code of any two packages can not normally run in the same process, since they need to run as different Linux users. You can use the sharedUserId attribute in the AndroidManifest.xml's manifest tag of each package to have them assigned the same user ID. By doing this, for purposes of security the two packages are then treated as being the same application, with the same user ID and file permissions. Note that in order to retain security, only two applications signed with the same signature (and requesting the same sharedUserId) will be given the same user ID.

Any data stored by an application will be assigned that application's user ID, and not normally accessible to other packages. When creating a new file with getSharedPreferences(String, int), openFileOutput(String, int), or openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory), you can use the MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE flags to allow any other package to read/write the file. When setting these flags, the file is still owned by your application, but its global read and/or write permissions have been set appropriately so any other application can see it.

An open discussion on floods and climate change

It cannot be "business as usual" either at home or internationally when dealing with climate change, said newly-appointed Minister for Environment and Water Resources Vivian Balakrishnan.

Singaporeans, he said, will have to understand the "difficult policy choices and trade-offs" in dealing with this challenge, before coming to a consensus on how to tackle it.

"My intention is to make this process as open and transparent as possible so that Singaporeans understand what is at stake," he said, referring to a collective decision-making process.

This open-communication approach also applies to tackling situations such as flooding - one of the hot issues raised at the May 7 polls.

Dr Balakrishnan said there was a need to assess if weather patterns were changing. "If indeed it has changed, then our planning norms will also have to be modified and that's an engineering and technical issue."

He added: "Every time a flood occurs, it will be newsworthy.

"It will certainly place stress on me and all the staff involved so we mustn't lose sight of the bigger picture, which is to make sure that we have taken into account all the different variables and especially when the weather and climate are changing, review our plans and then to operationalise the plan."

But he added as they were dealing with the weather, there would always be a large element of uncertainty.

He said: "What can be done will be done, the parts which obviously cannot be done or which are beyond our control, we would have to humbly and honestly communicate and explain that to Singaporeans."

Dr Balakrishnan was speaking to the media on the sidelines of yesterday's ComChest Heartstrings Walk, which was attended by 5,000 people and raised more than S$1 million for beneficiaries. It was flagged off by Prime Minister Lee Hsien Loong.

After heading the Ministry of Comunity Development, Youth and Sports (MCYS) for seven years, Dr Balakrishnan said he was looking forward to the challenge of his new post, in his first remarks to the media since the new Cabinet line-up was announced.

"The MCYS has grown tremendously in the last seven years, so it has really been a stretch. The Ministry for Environment and Water Resources will be a completely different set of issues, so there's going to be no shortage of work and analysis and consultation and exercise of political wisdom and judgement and so on."

Some of his proudest moments at the MCYS, he said, included the 2005 introduction of the Community Care Endowment Fund (ComCare) that pays for programmes to help needy Singaporeans, and the move to recognise social work as a profession through accreditation.