Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out-of-bounds read in Kokkos_Layout.hpp #975

Closed
ibaned opened this issue Jul 25, 2017 · 3 comments
Closed

Out-of-bounds read in Kokkos_Layout.hpp #975

ibaned opened this issue Jul 25, 2017 · 3 comments
Assignees
Labels
Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos)
Milestone

Comments

@ibaned
Copy link
Contributor

ibaned commented Jul 25, 2017

Moving this from trilinos/Trilinos#1516 for @CamelliaDPG.

He wrote the following:

The order_dimensions() method implemented in Kokkos_Layout.hpp is as follows:

template< typename iTypeOrder , typename iTypeDimen >
KOKKOS_INLINE_FUNCTION static
LayoutStride order_dimensions( int const rank
                             , iTypeOrder const * const order
                             , iTypeDimen const * const dimen )
{
  LayoutStride tmp ;
  // Verify valid rank order:
  int check_input = ARRAY_LAYOUT_MAX_RANK < rank ? 0 : int( 1 << rank ) - 1 ;
  for ( int r = 0 ; r < ARRAY_LAYOUT_MAX_RANK ; ++r ) {
    tmp.dimension[r] = 0 ;
    tmp.stride[r]    = 0 ;
    check_input &= ~int( 1 << order[r] );
  }
  if ( 0 == check_input ) {
    size_t n = 1 ;
    for ( int r = 0 ; r < rank ; ++r ) {
      tmp.stride[ order[r] ] = n ;
      n *= ( dimen[order[r]] );
      tmp.dimension[r] = dimen[r];
    }
  }
  return tmp ;
}

Whenever rank != ARRAY_LAYOUT_MAX_RANK, that first for loop will read beyond the end of the order array. Proposed fix is as follows:

template< typename iTypeOrder , typename iTypeDimen >
  KOKKOS_INLINE_FUNCTION static
  LayoutStride order_dimensions( int const rank
                               , iTypeOrder const * const order
                               , iTypeDimen const * const dimen )
    {
      LayoutStride tmp ;
      // Verify valid rank order:
      int check_input = ARRAY_LAYOUT_MAX_RANK < rank ? 0 : int( 1 << rank ) - 1 ;
      for ( int r = 0 ; r < ARRAY_LAYOUT_MAX_RANK ; ++r ) {
        tmp.dimension[r] = 0 ;
        tmp.stride[r]    = 0 ;
      }
      for ( int r = 0; r < rank; ++r ) {
        check_input &= ~int( 1 << order[r] );
      }
      if ( 0 == check_input ) {
        size_t n = 1 ;
        for ( int r = 0 ; r < rank ; ++r ) {
          tmp.stride[ order[r] ] = n ;
          n *= ( dimen[order[r]] );
          tmp.dimension[r] = dimen[r];
        }
      }
      return tmp ;
    }
@ibaned ibaned added the Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos) label Jul 25, 2017
@ibaned ibaned added this to the 2017-August (end) milestone Jul 25, 2017
@ibaned ibaned self-assigned this Jul 25, 2017
@ibaned ibaned added Enhancement Improve existing capability; will potentially require voting and removed Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos) labels Jul 25, 2017
@ibaned
Copy link
Contributor Author

ibaned commented Jul 25, 2017

My guess is no one calls this with an array shorter than the max right now, so I'll relabel this an enhancement to allow the function to work with smaller arrays. If you know of a case where code inside Kokkos or Trilinos calls it with a shorter array, then its back to being a bug.

@ibaned ibaned changed the title Buffer Overflow in Kokkos_Layout.hpp Out-of-bounds read in Kokkos_Layout.hpp Jul 25, 2017
@ibaned
Copy link
Contributor Author

ibaned commented Jul 25, 2017

Also renamed because buffer overflow usually refers to an out-of-bounds write, not a read.

@crtrott crtrott added Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos) bug - fix pushed to develop branch and removed Enhancement Improve existing capability; will potentially require voting labels Jul 25, 2017
@crtrott
Copy link
Member

crtrott commented Jul 25, 2017

While nobody might be calling it, it is technically allowed so this is a bug (with maybe zero impact right now). Though I actually believe I do use it in LAMMPS with less than that. Most likely I am just lucky that the rest of the cache line is zeroed out.

@hcedwar hcedwar modified the milestones: 2017-July-end, 2017-September (mid) Jul 26, 2017
@crtrott crtrott closed this as completed Jul 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos)
Projects
None yet
Development

No branches or pull requests

3 participants