question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Writing more then 65kb results in corrupted data

See original GitHub issue

Bugreport

Writing more then 65kb results in corrupted data

reading the corrupt data results in Unexpected token [...] in JSON at position 65536

Data written

image

Data read

image

The Code

image Writing 30k Integers into the buffer

More Context

image

The Error

image

The Version

"react-native-keychain": "^3.0.0-rc.3"

react-native-keychain@^3.0.0-rc.3:
  version "3.0.0"
  resolved "https://registry.yarnpkg.com/react-native-keychain/-/react-native-keychain-3.0.0.tgz#29da1dfa43c2581f76bf9420914fd38a1558cf18"
  integrity sha512-0incABt1+aXsZvG34mDV57KKanSB+iMHmxvWv+N6lgpNLaSoqrCxazjbZdeqD4qJ7Z+Etp5CLf/4v1aI+sNLBw==

The Device

A real Android 6.0 Device using react-native debug mode(maybe this causes trouble?)

image

The Current Solution

Split up the data into buckets, have another key which indexes the buckets.

This reduces speed, since there are more read/write Operations.

Assume:

  • Bucketsize b
  • Datapoints n

Results in ceil(n/b) + 1 read/write operations

image

  • Blue = IndexService
  • Green = BucketServiceName
  • Red = BucketService
  • Grey = Result/Input
  • Bucketsize = 1

Note: since its not easy to determine the size of an Object in byte, you have to select a size for the buckets which you consider save not to surpass 65kb.

The Current Solution’s Code

Sorry for that mess, cleanup is not yet done - ping me here for a followup 😉

static readKeychain = async () => {
    console.log('readKeychain');
    const keyindex = await Keychain.getGenericPassword(VotesLocal.KEYCHAIN_INDEX_SERVICE);
    if (!keyindex) {
      return null;
    }
    let indexchain = JSON.parse(keyindex.password);
    console.log(`readKeychain: ${JSON.stringify(indexchain)}`);
    indexchain.d = [];
    await Promise.all(
      indexchain.i.map(async serviceId => {
        const service = VotesLocal.KEYCHAIN_VOTES_SERVICE + serviceId;
        const setData = await Keychain.getGenericPassword(service);
        console.log(`readKeychain: ${JSON.stringify(service)}`);
        console.log(`readKeychain: ${JSON.stringify(setData)}`);
        if (setData) {
          indexchain.d.push(...JSON.parse(setData.password));
        }
      }),
    );
    delete indexchain.i;
    console.log(`readKeychain: ${JSON.stringify(indexchain)}`);
    return indexchain;
  };

  static writeKeychain = async data => {
    console.log('writeKeychain');
    console.log(`writeKeychain: ${JSON.stringify(data)}`);
    let index = [];
    // Split Data into packages to avoid error on 65k
    while (data.d.length > 0) {
      const set = data.d.splice(0, VotesLocal.KEYCHAIN_MAXSIZE);
      const setServiceId = index.length;
      const setService = VotesLocal.KEYCHAIN_VOTES_SERVICE + setServiceId;
      console.log(`writeKeychain: ${JSON.stringify(setService)}`);
      console.log(`writeKeychain: ${JSON.stringify(set)}`);
      await Keychain.setGenericPassword(
        VotesLocal.KEYCHAIN_VOTES_KEY,
        JSON.stringify(set),
        setService,
      );
      index.push(setServiceId);
    }

    delete data.d;
    data.i = index;
    console.log(`writeKeychain: ${JSON.stringify(data)}`);
    // console.log(JSON.stringify(data));
    /*const dummy = [];
    for (var i = 0; i < 30000; i++) {
      dummy.push(i);
    }
    data.dummy = dummy;
    console.log(data);
    console.log(JSON.stringify(data));*/

    return await Keychain.setGenericPassword(
      VotesLocal.KEYCHAIN_INDEX_KEY,
      JSON.stringify(data),
      VotesLocal.KEYCHAIN_INDEX_SERVICE,
    );
  };

Furthermore

Since the corruption in the data seem to be random bytes (changing values), I consider it likely that this is a bufferoverflow?!

And yes - this library is made to contain credentials not data - I know that we abuse its scope 😉

(Possibly) Related

https://github.com/oblador/react-native-keychain/issues/120 https://github.com/oblador/react-native-keychain/issues/174 https://github.com/oblador/react-native-keychain/issues/208

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

3reactions
sebkcommented, May 27, 2019

I still have the same problem and also with the latest library versions the problem still exists.

Has somebody else an idea howto solve it instead splitting the reading and writing?

I guess it is also the same as https://github.com/oblador/react-native-keychain/issues/208 (?)

1reaction
blankey1337commented, Oct 27, 2019

Just ran into this on Android 7.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data corruption when multiple users perform read and write ...
Fixes an issue in which data corruption occurs when multiple users perform read and write operations to a shared file when SMB2 is...
Read more >
Data corruption - Wikipedia
Data corruption refers to errors in computer data that occur during writing, reading, storage, transmission, or processing, which introduce unintended changes ...
Read more >
What Is Data Corruption? How to Fix a Corrupted Hard Drive
Causes of Data Corruption​​ Data can become corrupted during writing, editing, or transfer to another drive. When a program writes incorrect data ...
Read more >
How to React When Your Data is Corrupted? - Ontrack
You are not alone. Data corruption is far more common than one might think. Since the beginning of the computer era, users have...
Read more >
What is Data Corruption and Can You Prevent It? - phoenixNAP
Most data corruptions occur when a file somehow flips or mixes its binary code (bits of 0s and 1s). Bits are mixed up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found