1. 16 8月, 2019 3 次提交
  2. 26 7月, 2019 1 次提交
  3. 26 5月, 2019 1 次提交
    • L
      iwlwifi: mvm: check for length correctness in iwl_mvm_create_skb() · 2da19da7
      Luca Coelho 提交于
      [ Upstream commit de1887c064b9996ac03120d90d0a909a3f678f98 ]
      
      We don't check for the validity of the lengths in the packet received
      from the firmware.  If the MPDU length received in the rx descriptor
      is too short to contain the header length and the crypt length
      together, we may end up trying to copy a negative number of bytes
      (headlen - hdrlen < 0) which will underflow and cause us to try to
      copy a huge amount of data.  This causes oopses such as this one:
      
      BUG: unable to handle kernel paging request at ffff896be2970000
      PGD 5e201067 P4D 5e201067 PUD 5e205067 PMD 16110d063 PTE 8000000162970161
      Oops: 0003 [#1] PREEMPT SMP NOPTI
      CPU: 2 PID: 1824 Comm: irq/134-iwlwifi Not tainted 4.19.33-04308-geea41cf4930f #1
      Hardware name: [...]
      RIP: 0010:memcpy_erms+0x6/0x10
      Code: 90 90 90 90 eb 1e 0f 1f 00 48 89 f8 48 89 d1 48 c1 e9 03 83 e2 07 f3 48 a5 89 d1 f3 a4 c3 66 0f 1f 44 00 00 48 89 f8 48 89 d1 <f3> a4 c3
       0f 1f 80 00 00 00 00 48 89 f8 48 83 fa 20 72 7e 40 38 fe
      RSP: 0018:ffffa4630196fc60 EFLAGS: 00010287
      RAX: ffff896be2924618 RBX: ffff896bc8ecc600 RCX: 00000000fffb4610
      RDX: 00000000fffffff8 RSI: ffff896a835e2a38 RDI: ffff896be2970000
      RBP: ffffa4630196fd30 R08: ffff896bc8ecc600 R09: ffff896a83597000
      R10: ffff896bd6998400 R11: 000000000200407f R12: ffff896a83597050
      R13: 00000000fffffff8 R14: 0000000000000010 R15: ffff896a83597038
      FS:  0000000000000000(0000) GS:ffff896be8280000(0000) knlGS:0000000000000000
      CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
      CR2: ffff896be2970000 CR3: 000000005dc12002 CR4: 00000000003606e0
      DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
      DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
      Call Trace:
       iwl_mvm_rx_mpdu_mq+0xb51/0x121b [iwlmvm]
       iwl_pcie_rx_handle+0x58c/0xa89 [iwlwifi]
       iwl_pcie_irq_rx_msix_handler+0xd9/0x12a [iwlwifi]
       irq_thread_fn+0x24/0x49
       irq_thread+0xb0/0x122
       kthread+0x138/0x140
       ret_from_fork+0x1f/0x40
      
      Fix that by checking the lengths for correctness and trigger a warning
      to show that we have received wrong data.
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NSasha Levin <sashal@kernel.org>
      2da19da7
  4. 06 4月, 2019 1 次提交
  5. 13 2月, 2019 2 次提交
  6. 26 1月, 2019 1 次提交
  7. 29 12月, 2018 1 次提交
  8. 01 12月, 2018 4 次提交
    • L
      iwlwifi: mvm: don't use SAR Geo if basic SAR is not used · c74c926f
      Luca Coelho 提交于
      commit 5d041c46ccb9b48acc110e214beff5e2789311df upstream.
      
      We can't use SAR Geo if basic SAR is not enabled, since the SAR Geo
      tables define offsets in relation to the basic SAR table in use.
      
      To fix this, make iwl_mvm_sar_init() return one in case WRDS is not
      available, so we can skip reading WGDS entirely.
      
      Fixes: a6bff3cb ("iwlwifi: mvm: add GEO_TX_POWER_LIMIT cmd for geographic tx power table")
      Cc: stable@vger.kernel.org # 4.12+
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      c74c926f
    • E
      iwlwifi: mvm: fix regulatory domain update when the firmware starts · 49697515
      Emmanuel Grumbach 提交于
      commit 82715ac71e6b94a2c2136e31f3a8e6748e33aa8c upstream.
      
      When the firmware starts, it doesn't have any regulatory
      information, hence it uses the world wide limitations. The
      driver can feed the firmware with previous knowledge that
      was kept in the driver, but the firmware may still not
      update its internal tables.
      
      This happens when we start a BSS interface, and then the
      firmware can change the regulatory tables based on our
      location and it'll use more lenient, location specific
      rules. Then, if the firmware is shut down (when the
      interface is brought down), and then an AP interface is
      created, the firmware will forget the country specific
      rules.
      
      The host will think that we are in a certain country that
      may allow channels and will try to teach the firmware about
      our location, but the firmware may still not allow to drop
      the world wide limitations and apply country specific rules
      because it was just re-started.
      
      In this case, the firmware will reply with MCC_RESP_ILLEGAL
      to the MCC_UPDATE_CMD. In that case, iwlwifi needs to let
      the upper layers (cfg80211 / hostapd) know that the channel
      list they know about has been updated.
      
      This fixes https://bugzilla.kernel.org/show_bug.cgi?id=201105
      
      Cc: stable@vger.kernel.org
      Signed-off-by: NEmmanuel Grumbach <emmanuel.grumbach@intel.com>
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      49697515
    • E
      iwlwifi: mvm: support sta_statistics() even on older firmware · b643d705
      Emmanuel Grumbach 提交于
      commit ec484d03ef0df8d34086b95710e355a259cbe1f2 upstream.
      
      The oldest firmware supported by iwlmvm do support getting
      the average beacon RSSI. Enable the sta_statistics() call
      from mac80211 even on older firmware versions.
      
      Fixes: 33cef925 ("iwlwifi: mvm: support beacon statistics for BSS client")
      Cc: stable@vger.kernel.org # 4.2+
      Signed-off-by: NEmmanuel Grumbach <emmanuel.grumbach@intel.com>
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      b643d705
    • M
      iwlwifi: fix wrong WGDS_WIFI_DATA_SIZE · 29d920ba
      Matt Chen 提交于
      commit 66e839030fd698586734e017fd55c4f2a89dba0b upstream.
      
      From coreboot/BIOS:
      Name ("WGDS", Package() {
       Revision,
       Package() {
           DomainType,                         // 0x7:WiFi ==> We miss this one.
           WgdsWiFiSarDeltaGroup1PowerMax1,    // Group 1 FCC 2400 Max
           WgdsWiFiSarDeltaGroup1PowerChainA1, // Group 1 FCC 2400 A Offset
           WgdsWiFiSarDeltaGroup1PowerChainB1, // Group 1 FCC 2400 B Offset
           WgdsWiFiSarDeltaGroup1PowerMax2,    // Group 1 FCC 5200 Max
           WgdsWiFiSarDeltaGroup1PowerChainA2, // Group 1 FCC 5200 A Offset
           WgdsWiFiSarDeltaGroup1PowerChainB2, // Group 1 FCC 5200 B Offset
           WgdsWiFiSarDeltaGroup2PowerMax1,    // Group 2 EC Jap 2400 Max
           WgdsWiFiSarDeltaGroup2PowerChainA1, // Group 2 EC Jap 2400 A Offset
           WgdsWiFiSarDeltaGroup2PowerChainB1, // Group 2 EC Jap 2400 B Offset
           WgdsWiFiSarDeltaGroup2PowerMax2,    // Group 2 EC Jap 5200 Max
           WgdsWiFiSarDeltaGroup2PowerChainA2, // Group 2 EC Jap 5200 A Offset
           WgdsWiFiSarDeltaGroup2PowerChainB2, // Group 2 EC Jap 5200 B Offset
           WgdsWiFiSarDeltaGroup3PowerMax1,    // Group 3 ROW 2400 Max
           WgdsWiFiSarDeltaGroup3PowerChainA1, // Group 3 ROW 2400 A Offset
           WgdsWiFiSarDeltaGroup3PowerChainB1, // Group 3 ROW 2400 B Offset
           WgdsWiFiSarDeltaGroup3PowerMax2,    // Group 3 ROW 5200 Max
           WgdsWiFiSarDeltaGroup3PowerChainA2, // Group 3 ROW 5200 A Offset
           WgdsWiFiSarDeltaGroup3PowerChainB2, // Group 3 ROW 5200 B Offset
       }
      })
      
      When read the ACPI data to find out the WGDS, the DATA_SIZE is never
      matched.
      From the above format, it gives 19 numbers, but our driver is hardcode
      as 18.
      Fix it to pass then can parse the data into our wgds table.
      Then we will see:
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init Sending GEO_TX_POWER_LIMIT
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init SAR geographic profile[0]
      Band[0]: chain A = 68 chain B = 69 max_tx_power = 54
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init SAR geographic profile[0]
      Band[1]: chain A = 48 chain B = 49 max_tx_power = 70
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init SAR geographic profile[1]
      Band[0]: chain A = 51 chain B = 67 max_tx_power = 50
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init SAR geographic profile[1]
      Band[1]: chain A = 69 chain B = 70 max_tx_power = 68
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init SAR geographic profile[2]
      Band[0]: chain A = 49 chain B = 50 max_tx_power = 48
      iwlwifi 0000:01:00.0: U iwl_mvm_sar_geo_init SAR geographic profile[2]
      Band[1]: chain A = 52 chain B = 53 max_tx_power = 51
      
      Cc: stable@vger.kernel.org # 4.12+
      Fixes: a6bff3cb ("iwlwifi: mvm: add GEO_TX_POWER_LIMIT cmd for geographic tx power table")
      Signed-off-by: NMatt Chen <matt.chen@intel.com>
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      29d920ba
  9. 14 11月, 2018 4 次提交
    • L
      iwlwifi: mvm: check return value of rs_rate_from_ucode_rate() · 3fa27214
      Luca Coelho 提交于
      commit 3d71c3f1f50cf309bd20659422af549bc784bfff upstream.
      
      The rs_rate_from_ucode_rate() function may return -EINVAL if the rate
      is invalid, but none of the callsites check for the error, potentially
      making us access arrays with index IWL_RATE_INVALID, which is larger
      than the arrays, causing an out-of-bounds access.  This will trigger
      KASAN warnings, such as the one reported in the bugzilla issue
      mentioned below.
      
      This fixes https://bugzilla.kernel.org/show_bug.cgi?id=200659
      
      Cc: stable@vger.kernel.org
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NKalle Valo <kvalo@codeaurora.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      3fa27214
    • S
      iwlwifi: mvm: fix BAR seq ctrl reporting · 8ba36c16
      Sara Sharon 提交于
      [ Upstream commit 941ab4eb ]
      
      There is a bug in FW where the sequence control may be
      incorrect, and the driver overrides it with the value
      of the ieee80211 header.
      
      However, in BAR there is no sequence control in the header,
      which result with arbitrary sequence.
      
      This access to an unknown location is bad and it makes the
      logs very confusing - so fix it.
      Signed-off-by: NSara Sharon <sara.sharon@intel.com>
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NSasha Levin <sashal@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      8ba36c16
    • L
      iwlwifi: mvm: check for n_profiles validity in EWRD ACPI · a2abae52
      Luca Coelho 提交于
      [ Upstream commit 2e1976bb75263fbad918e82184b16a23bd721546 ]
      
      When reading the profiles from the EWRD table in ACPI, we loop over
      the data and set it into our internal table.  We use the number of
      profiles specified in ACPI without checking its validity, so if the
      ACPI table is corrupted and the number is larger than our array size,
      we will try to make an out-of-bounds access.
      
      Fix this by making sure the value specified in the ACPI table is
      valid.
      
      Fixes: 69964905 ("iwlwifi: mvm: add support for EWRD (Dynamic SAR) ACPI table")
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NSasha Levin <sashal@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a2abae52
    • E
      iwlwifi: mvm: clear HW_RESTART_REQUESTED when stopping the interface · b325d550
      Emmanuel Grumbach 提交于
      [ Upstream commit 155f7e0441cd121b1e673d465a35e99f4b9b2f0b ]
      
      Fix a bug that happens in the following scenario:
      1) suspend without WoWLAN
      2) mac80211 calls drv_stop because of the suspend
      3) __iwl_mvm_mac_stop deallocates the aux station
      4) during drv_stop the firmware crashes
      5) iwlmvm:
      	* sets IWL_MVM_STATUS_HW_RESTART_REQUESTED
      	* asks mac80211 to kick the restart flow
      6) mac80211 puts the restart worker into a freezable
         queue which means that the worker will not run for now
         since the workqueue is already frozen
      7) ...
      8) resume
      9) mac80211 runs ieee80211_reconfig as part of the resume
      10) mac80211 detects that a restart flow has been requested
          and that we are now resuming from suspend and cancels
          the restart worker
      11) mac80211 calls drv_start()
      12) __iwl_mvm_mac_start checks that IWL_MVM_STATUS_HW_RESTART_REQUESTED
          clears it, sets IWL_MVM_STATUS_IN_HW_RESTART and calls
          iwl_mvm_restart_cleanup()
      13) iwl_fw_error_dump gets called and accesses the device
          to get debug data
      14) iwl_mvm_up adds the aux station
      15) iwl_mvm_add_aux_sta() allocates an internal station for
          the aux station
      16) iwl_mvm_allocate_int_sta() tests IWL_MVM_STATUS_IN_HW_RESTART
          and doesn't really allocate a station ID for the aux
          station
      17) a new queue is added for the aux station
      
      Note that steps from 5 to 9 aren't really part of the
      problem but were described for the sake of completeness.
      
      Once the iwl_mvm_mac_stop() is called, the device is not
      accessible, meaning that step 12) can't succeed and we'll
      see the following:
      
      drivers/net/wireless/intel/iwlwifi/pcie/trans.c:2122 iwl_trans_pcie_grab_nic_access+0xc0/0x1d6 [iwlwifi]()
      Timeout waiting for hardware access (CSR_GP_CNTRL 0x080403d8)
      Call Trace:
      [<ffffffffc03e6ad3>] iwl_trans_pcie_grab_nic_access+0xc0/0x1d6 [iwlwifi]
      [<ffffffffc03e6a13>] iwl_trans_pcie_dump_regs+0x3fd/0x3fd [iwlwifi]
      [<ffffffffc03dad42>] iwl_fw_error_dump+0x4f5/0xe8b [iwlwifi]
      [<ffffffffc04bd43e>] __iwl_mvm_mac_start+0x5a/0x21a [iwlmvm]
      [<ffffffffc04bd6d2>] iwl_mvm_mac_start+0xd4/0x103 [iwlmvm]
      [<ffffffffc042d378>] drv_start+0xa1/0xc5 [iwl7000_mac80211]
      [<ffffffffc045a339>] ieee80211_reconfig+0x145/0xf50 [mac80211]
      [<ffffffffc044788b>] ieee80211_resume+0x62/0x66 [mac80211]
      [<ffffffffc0366c5b>] wiphy_resume+0xa9/0xc6 [cfg80211]
      
      The station id of the aux station is set to 0xff in step 3
      and because we don't really allocate a new station id for
      the auxliary station (as explained in 16), we end up sending
      a command to the firmware asking to connect the queue
      to station id 0xff. This makes the firmware crash with the
      following information:
      
      0x00002093 | ADVANCED_SYSASSERT
      0x000002F0 | trm_hw_status0
      0x00000000 | trm_hw_status1
      0x00000B38 | branchlink2
      0x0001978C | interruptlink1
      0x00000000 | interruptlink2
      0xFF080501 | data1
      0xDEADBEEF | data2
      0xDEADBEEF | data3
      Firmware error during reconfiguration - reprobe!
      FW error in SYNC CMD SCD_QUEUE_CFG
      
      Fix this by clearing IWL_MVM_STATUS_HW_RESTART_REQUESTED
      in iwl_mvm_mac_stop(). We won't be able to collect debug
      data anyway and when we will brought up again, we will
      have a clean state from the firmware perspective.
      Since we won't have IWL_MVM_STATUS_IN_HW_RESTART set in
      step 12) we won't get to the 2093 ASSERT either.
      
      Fixes: bf8b286f ("iwlwifi: mvm: defer setting IWL_MVM_STATUS_IN_HW_RESTART")
      Signed-off-by: NEmmanuel Grumbach <emmanuel.grumbach@intel.com>
      Signed-off-by: NLuca Coelho <luciano.coelho@intel.com>
      Signed-off-by: NSasha Levin <sashal@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      b325d550
  10. 02 8月, 2018 6 次提交
  11. 26 7月, 2018 7 次提交
  12. 28 6月, 2018 1 次提交
    • O
      wireless-drivers: use BIT_ULL for NL80211_STA_INFO_ attribute types · 22d0d2fa
      Omer Efrat 提交于
      The BIT macro uses unsigned long which some architectures handle as 32 bit
      and therefore might cause macro's shift to overflow when used on a value
      equals or larger than 32 (NL80211_STA_INFO_RX_DURATION and afterwards).
      
      Since 'filled' member in station_info changed to u64, BIT_ULL macro
      should be used with all NL80211_STA_INFO_* attribute types instead of BIT
      to prevent future possible bugs when one will use BIT macro for higher
      attributes by mistake.
      
      This commit cleans up all usages of BIT macro with the above field
      in wireless-drivers by changing it to BIT_ULL instead. In addition, there are
      some places which don't use BIT nor BIT_ULL macros so align those as well.
      Signed-off-by: NOmer Efrat <omer.efrat@tandemg.com>
      Signed-off-by: NKalle Valo <kvalo@codeaurora.org>
      22d0d2fa
  13. 13 6月, 2018 1 次提交
    • K
      treewide: kzalloc() -> kcalloc() · 6396bb22
      Kees Cook 提交于
      The kzalloc() function has a 2-factor argument form, kcalloc(). This
      patch replaces cases of:
      
              kzalloc(a * b, gfp)
      
      with:
              kcalloc(a * b, gfp)
      
      as well as handling cases of:
      
              kzalloc(a * b * c, gfp)
      
      with:
      
              kzalloc(array3_size(a, b, c), gfp)
      
      as it's slightly less ugly than:
      
              kzalloc_array(array_size(a, b), c, gfp)
      
      This does, however, attempt to ignore constant size factors like:
      
              kzalloc(4 * 1024, gfp)
      
      though any constants defined via macros get caught up in the conversion.
      
      Any factors with a sizeof() of "unsigned char", "char", and "u8" were
      dropped, since they're redundant.
      
      The Coccinelle script used for this was:
      
      // Fix redundant parens around sizeof().
      @@
      type TYPE;
      expression THING, E;
      @@
      
      (
        kzalloc(
      -	(sizeof(TYPE)) * E
      +	sizeof(TYPE) * E
        , ...)
      |
        kzalloc(
      -	(sizeof(THING)) * E
      +	sizeof(THING) * E
        , ...)
      )
      
      // Drop single-byte sizes and redundant parens.
      @@
      expression COUNT;
      typedef u8;
      typedef __u8;
      @@
      
      (
        kzalloc(
      -	sizeof(u8) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(__u8) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(char) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(unsigned char) * (COUNT)
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(u8) * COUNT
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(__u8) * COUNT
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(char) * COUNT
      +	COUNT
        , ...)
      |
        kzalloc(
      -	sizeof(unsigned char) * COUNT
      +	COUNT
        , ...)
      )
      
      // 2-factor product with sizeof(type/expression) and identifier or constant.
      @@
      type TYPE;
      expression THING;
      identifier COUNT_ID;
      constant COUNT_CONST;
      @@
      
      (
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * (COUNT_ID)
      +	COUNT_ID, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * COUNT_ID
      +	COUNT_ID, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * (COUNT_CONST)
      +	COUNT_CONST, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * COUNT_CONST
      +	COUNT_CONST, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * (COUNT_ID)
      +	COUNT_ID, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * COUNT_ID
      +	COUNT_ID, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * (COUNT_CONST)
      +	COUNT_CONST, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * COUNT_CONST
      +	COUNT_CONST, sizeof(THING)
        , ...)
      )
      
      // 2-factor product, only identifiers.
      @@
      identifier SIZE, COUNT;
      @@
      
      - kzalloc
      + kcalloc
        (
      -	SIZE * COUNT
      +	COUNT, SIZE
        , ...)
      
      // 3-factor product with 1 sizeof(type) or sizeof(expression), with
      // redundant parens removed.
      @@
      expression THING;
      identifier STRIDE, COUNT;
      type TYPE;
      @@
      
      (
        kzalloc(
      -	sizeof(TYPE) * (COUNT) * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE) * (COUNT) * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE) * COUNT * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE) * COUNT * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(TYPE))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * (COUNT) * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * (COUNT) * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * COUNT * (STRIDE)
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      |
        kzalloc(
      -	sizeof(THING) * COUNT * STRIDE
      +	array3_size(COUNT, STRIDE, sizeof(THING))
        , ...)
      )
      
      // 3-factor product with 2 sizeof(variable), with redundant parens removed.
      @@
      expression THING1, THING2;
      identifier COUNT;
      type TYPE1, TYPE2;
      @@
      
      (
        kzalloc(
      -	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
        , ...)
      |
        kzalloc(
      -	sizeof(THING1) * sizeof(THING2) * COUNT
      +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
        , ...)
      |
        kzalloc(
      -	sizeof(THING1) * sizeof(THING2) * (COUNT)
      +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE1) * sizeof(THING2) * COUNT
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
        , ...)
      |
        kzalloc(
      -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
      +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
        , ...)
      )
      
      // 3-factor product, only identifiers, with redundant parens removed.
      @@
      identifier STRIDE, SIZE, COUNT;
      @@
      
      (
        kzalloc(
      -	(COUNT) * STRIDE * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * (STRIDE) * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * STRIDE * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	(COUNT) * (STRIDE) * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * (STRIDE) * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	(COUNT) * STRIDE * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	(COUNT) * (STRIDE) * (SIZE)
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      |
        kzalloc(
      -	COUNT * STRIDE * SIZE
      +	array3_size(COUNT, STRIDE, SIZE)
        , ...)
      )
      
      // Any remaining multi-factor products, first at least 3-factor products,
      // when they're not all constants...
      @@
      expression E1, E2, E3;
      constant C1, C2, C3;
      @@
      
      (
        kzalloc(C1 * C2 * C3, ...)
      |
        kzalloc(
      -	(E1) * E2 * E3
      +	array3_size(E1, E2, E3)
        , ...)
      |
        kzalloc(
      -	(E1) * (E2) * E3
      +	array3_size(E1, E2, E3)
        , ...)
      |
        kzalloc(
      -	(E1) * (E2) * (E3)
      +	array3_size(E1, E2, E3)
        , ...)
      |
        kzalloc(
      -	E1 * E2 * E3
      +	array3_size(E1, E2, E3)
        , ...)
      )
      
      // And then all remaining 2 factors products when they're not all constants,
      // keeping sizeof() as the second factor argument.
      @@
      expression THING, E1, E2;
      type TYPE;
      constant C1, C2, C3;
      @@
      
      (
        kzalloc(sizeof(THING) * C2, ...)
      |
        kzalloc(sizeof(TYPE) * C2, ...)
      |
        kzalloc(C1 * C2 * C3, ...)
      |
        kzalloc(C1 * C2, ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * (E2)
      +	E2, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(TYPE) * E2
      +	E2, sizeof(TYPE)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * (E2)
      +	E2, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	sizeof(THING) * E2
      +	E2, sizeof(THING)
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	(E1) * E2
      +	E1, E2
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	(E1) * (E2)
      +	E1, E2
        , ...)
      |
      - kzalloc
      + kcalloc
        (
      -	E1 * E2
      +	E1, E2
        , ...)
      )
      Signed-off-by: NKees Cook <keescook@chromium.org>
      6396bb22
  14. 07 6月, 2018 1 次提交
    • K
      treewide: Use struct_size() for kmalloc()-family · acafe7e3
      Kees Cook 提交于
      One of the more common cases of allocation size calculations is finding
      the size of a structure that has a zero-sized array at the end, along
      with memory for some number of elements for that array. For example:
      
      struct foo {
          int stuff;
          void *entry[];
      };
      
      instance = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);
      
      Instead of leaving these open-coded and prone to type mistakes, we can
      now use the new struct_size() helper:
      
      instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);
      
      This patch makes the changes for kmalloc()-family (and kvmalloc()-family)
      uses. It was done via automatic conversion with manual review for the
      "CHECKME" non-standard cases noted below, using the following Coccinelle
      script:
      
      // pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
      //                      sizeof *pkey_cache->table, GFP_KERNEL);
      @@
      identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
      expression GFP;
      identifier VAR, ELEMENT;
      expression COUNT;
      @@
      
      - alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
      + alloc(struct_size(VAR, ELEMENT, COUNT), GFP)
      
      // mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
      @@
      identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
      expression GFP;
      identifier VAR, ELEMENT;
      expression COUNT;
      @@
      
      - alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
      + alloc(struct_size(VAR, ELEMENT, COUNT), GFP)
      
      // Same pattern, but can't trivially locate the trailing element name,
      // or variable name.
      @@
      identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
      expression GFP;
      expression SOMETHING, COUNT, ELEMENT;
      @@
      
      - alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
      + alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)
      Signed-off-by: NKees Cook <keescook@chromium.org>
      acafe7e3
  15. 30 5月, 2018 6 次提交