Concurrent Execution Using Shared Resource with Improper Synchronization ('Race Condition')
The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
This can have security implications when the expected synchronization is in security-critical code, such as recording whether a user is authenticated or modifying important state information that should not be influenced by an outsider.
Como corrigir esta vulnerabilidade
Estratégias de prevenção para Race Condition baseadas em 6 regras de detecção do Shoulder.
Protect concurrent slice access with mutex or use channels to collect results
func collect(items []string) []string { - var results []string - for _, item := range items { - go func(i string) { - results = append(results, process(i)) - }(item) - } - time.Sleep(time.Second) + resultsCh := make(chan string, len(items)) + for _, item := range items { + go func(i string) { + resultsCh <- process(i) + }(item) + } + results := make([]string, 0, len(items)) + for i := 0; i < len(items); i++ { + results = append(results, <-resultsCh) + } return results }
Use thread-safe accessor methods or sync.RWMutex for concurrent map access
func getNodeName(node *Node) string { - name, ok := node.Attributes["name"].(string) + name, ok := node.GetAttrString("name") if !ok { return "" } return name }
Protect shared state with sync.Mutex, atomic operations, or sync.Map
- var counter int - func increment() { - go func() { - counter++ + var counter int64 + func increment() { + go func() { + atomic.AddInt64(&counter, 1) }() }
Use database transactions with row-level locking for atomic read-modify-write operations
app.post('/withdraw', async (req, res) => { - const account = await Account.findOne({ where: { userId } }); - if (account.balance >= amount) { - await account.update({ balance: account.balance - amount }); + const transaction = await db.transaction(); + try { + const account = await Account.findOne({ + where: { userId }, + lock: transaction.LOCK.UPDATE, + transaction + }); + if (account.balance < amount) { + await transaction.rollback(); + return res.status(400).json({ error: 'Insufficient funds' }); + } + await account.update({ balance: account.balance - amount }, { transaction }); + await transaction.commit(); + } catch (e) { + await transaction.rollback(); + throw e; } });
Use locks for shared data and atomic operations for file access
import threading counter = 0 - - def increment(): - global counter - counter += 1 + lock = threading.Lock() + + def increment(): + global counter + with lock: + counter += 1 threads = [threading.Thread(target=increment) for _ in range(100)]
Práticas-chave
- Use data races, lost data, or panics
- Use race conditions
- Use of sync
Encontre vulnerabilidades no seu código
Use o Shoulder para escanear seu código em busca de padrões Concurrent Execution Using Shared Resource with Improper Synchronization ('Race Condition'). 6 regras.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=362 # Or scan entire project npx @shoulderdev/cli trust .
Regras de Detecção (6)
O que observar nas revisões de código
Estes padrões indicam vulnerabilidades potenciais de Concurrent Execution Using Shared Resource with Improper Synchronization ('Race Condition'). Procure-os durante revisões de código e auditorias de segurança.
Escaneie seu código para Concurrent Execution Using Shared Resource with Improper Synchronization ('Race Condition')
O Shoulder CLI encontra padrões vulneráveis em todo o seu código.